how to get the length of a number

Cameron Laird claird at lairds.us
Mon Jun 12 05:34:51 CEST 2006


In article <mailman.6895.1150058832.27775.python-list at python.org>,
Felipe Almeida Lessa  <felipe.lessa at gmail.com> 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)).



More information about the Python-list mailing list