how to get the length of a number

S

Stan Cook

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
 
S

Saketh

Stan said:
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.
 
S

Stan Cook

Saketh said:
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
 
S

Sybren Stuvel

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.
5.0

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

Sybren
 
F

Felipe Almeida Lessa

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))
 
C

Claudio Grondi

Stan said:
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
 
F

Felipe Almeida Lessa

Em Dom, 2006-06-11 às 13:17 -0700, Saketh escreveu:
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
 
F

Felipe Almeida Lessa

Em Dom, 2006-06-11 às 22:33 +0200, Sybren Stuvel escreveu:
Felipe Almeida Lessa enlightened us with:

That doesn't work properly.

5.0

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

Hmmm, you're right.

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

Cameron Laird

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

.... and you're restricting to the positive integers, I take it?
I still have rounding problems: ... return math.floor(math.log(x, 10)) + 1
... 8.0

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top