Velocity Reviews > how to get the length of a number

# how to get the length of a number

Stan Cook
Guest
Posts: n/a

 06-11-2006
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

Saketh
Guest
Posts: n/a

 06-11-2006

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.

Fredrik Lundh
Guest
Posts: n/a

 06-11-2006
Stan Cook wrote:

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

str(value)

</F>

Stan Cook
Guest
Posts: n/a

 06-11-2006
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

Sybren Stuvel
Guest
Posts: n/a

 06-11-2006
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

Felipe Almeida Lessa
Guest
Posts: n/a

 06-11-2006
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.

Claudio Grondi
Guest
Posts: n/a

 06-11-2006
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

Fredrik Lundh
Guest
Posts: n/a

 06-11-2006
Felipe Almeida Lessa wrote:

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

x = 0

</F>

Felipe Almeida Lessa
Guest
Posts: n/a

 06-11-2006
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
Guest
Posts: n/a

 06-11-2006
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.