how to get the length of a number
Felipe Almeida Lessa
felipe.lessa at gmail.com
Sun Jun 11 23:11:51 CEST 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.
More information about the Python-list
mailing list