# how to get the length of a number

Discussion in 'Python' started by Stan Cook, Jun 11, 2006.

1. ### Stan CookGuest

Can anyone tell me how to get the length of a number. I
know len(string) will get the length of a string, but it
doesn't like len(int). I seem to remember something like %s
string. I tried to set a variable = to %s int, but that
doesn't work. Is there a function I've forgotten about to
convert an integer to a string?

Regards

Stan

Stan Cook, Jun 11, 2006

2. ### SakethGuest

Stan Cook wrote:
> Can anyone tell me how to get the length of a number. I
> know len(string) will get the length of a string, but it
> doesn't like len(int). I seem to remember something like %s
> string. I tried to set a variable = to %s int, but that
> doesn't work. Is there a function I've forgotten about to
> convert an integer to a string?
>
> Regards
>
> Stan

Use str(int). Then use len(). For example, len(str(12345)) will give
you 5.

Saketh, Jun 11, 2006

3. ### Fredrik LundhGuest

Stan Cook wrote:

> Is there a function I've forgotten about to convert an
> integer to a string?

str(value)

</F>

Fredrik Lundh, Jun 11, 2006
4. ### Stan CookGuest

Saketh wrote:
> Stan Cook wrote:
>> Can anyone tell me how to get the length of a number. I
>> know len(string) will get the length of a string, but it
>> doesn't like len(int). I seem to remember something like %s
>> string. I tried to set a variable = to %s int, but that
>> doesn't work. Is there a function I've forgotten about to
>> convert an integer to a string?
>>
>> Regards
>>
>> Stan

>
> Use str(int). Then use len(). For example, len(str(12345)) will give
> you 5.
>

Ahhhh! How could I have forgotten that. Boy is my face red

Thanks....

I'll try to make my next question a more intelligent one.

Regards,

Stan

Stan Cook, Jun 11, 2006
5. ### Sybren StuvelGuest

Felipe Almeida Lessa enlightened us with:
> To see how many decimal digits it has:
>
> import math
> math.ceil(math.log(i, 10))

That doesn't work properly.

>>> import math
>>> math.ceil(math.log(10000, 10))

4.0
>>> math.ceil(math.log(10001, 10))

5.0

But "10000" certainly has as much digits as "10001".

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa

Sybren Stuvel, Jun 11, 2006
6. ### Felipe Almeida LessaGuest

Em Dom, 2006-06-11 Ã s 20:10 +0000, Stan Cook escreveu:
> Can anyone tell me how to get the length of a number. I
> know len(string) will get the length of a string, but it
> doesn't like len(int). I seem to remember something like %s
> string. I tried to set a variable = to %s int, but that
> doesn't work. Is there a function I've forgotten about to
> convert an integer to a string?

To convert an integer i to a string:

str(i) or "%s" % i

To see how many decimal digits it has:

import math
math.ceil(math.log(i, 10))

--
Felipe.

Felipe Almeida Lessa, Jun 11, 2006
7. ### Claudio GrondiGuest

Stan Cook wrote:
> Can anyone tell me how to get the length of a number. I know
> len(string) will get the length of a string, but it doesn't like
> len(int). I seem to remember something like %s string. I tried to set
> a variable = to %s int, but that doesn't work. Is there a function I've
> forgotten about to convert an integer to a string?
>
> Regards
>
> Stan

len('%s'%(1234567,)) gives 7
len('%s'%(1234**45,)) and len(str(1234**45))
give 140

Claudio

Claudio Grondi, Jun 11, 2006
8. ### Fredrik LundhGuest

Felipe Almeida Lessa wrote:

> Hmmm, you're right.
>
> math.floor(math.log(x, 10)) + 1

x = 0

</F>

Fredrik Lundh, Jun 11, 2006
9. ### Felipe Almeida LessaGuest

Em Dom, 2006-06-11 Ã s 13:17 -0700, Saketh escreveu:
> Stan Cook wrote:
> > Can anyone tell me how to get the length of a number. I
> > know len(string) will get the length of a string, but it
> > doesn't like len(int). I seem to remember something like %s
> > string. I tried to set a variable = to %s int, but that
> > doesn't work. Is there a function I've forgotten about to
> > convert an integer to a string?
> >
> > Regards
> >
> > Stan

>
> Use str(int). Then use len(). For example, len(str(12345)) will give
> you 5.

\$ python2.4 -mtimeit -s 'x=12345' 'len(str(x))' 1000000 loops, best of
3: 1.33 usec per loop
\$ python2.4 -mtimeit -s 'x=12345;from math import ceil,log' 'ceil(log(x,
10))'
1000000 loops, best of 3: 1.54 usec per loop
\$ python2.4 -mtimeit -s 'x=12345**123' 'len(str(x))' 1000 loops, best of
3: 209 usec per loop
\$ python2.4 -mtimeit -s 'x=12345**123;from math import ceil,log'
'ceil(log(x, 10))'
1000000 loops, best of 3: 1.55 usec per loop
\$ python2.4 -mtimeit -s 'x=12345**1234' 'len(str(x))' 100 loops, best of
3: 19.2 msec per loop
\$ python2.4 -mtimeit -s 'x=12345**1234;from math import ceil,log'
'ceil(log(x, 10))'
1000000 loops, best of 3: 1.53 usec per loop

--
Felipe.

Felipe Almeida Lessa, Jun 11, 2006
10. ### Felipe Almeida LessaGuest

Em Dom, 2006-06-11 Ã s 22:33 +0200, Sybren Stuvel escreveu:
> Felipe Almeida Lessa enlightened us with:
> > To see how many decimal digits it has:
> >
> > import math
> > math.ceil(math.log(i, 10))

>
> That doesn't work properly.
>
> >>> import math
> >>> math.ceil(math.log(10000, 10))

> 4.0
> >>> math.ceil(math.log(10001, 10))

> 5.0
>
> But "10000" certainly has as much digits as "10001".

Hmmm, you're right.

math.floor(math.log(x, 10)) + 1

--
Felipe.

Felipe Almeida Lessa, Jun 11, 2006
11. ### Cameron LairdGuest

In article <>,
Felipe Almeida Lessa <> wrote:
.
.
.
>math.floor(math.log(x, 10)) + 1
>
>--
>Felipe.
>

.... and you're restricting to the positive integers, I take it?
I still have rounding problems:
>>> def l(x):

... return math.floor(math.log(x, 10)) + 1
...
>>> l(10)

2.0
>>> l(100)

3.0
>>> l(1000)

3.0
>>> l(10000)

5.0
>>> l(100000)

6.0
>>> l(1000000)

6.0
>>> l(10000000)

8.0

While modest mollification improves these results, it's going
to be tough to beat len(str(x)).

Cameron Laird, Jun 12, 2006