Rounding up to the nearest exact logarithmic decade

johnzenger at gmail.com johnzenger at gmail.com
Tue Feb 28 18:26:36 EST 2006


I like Fredrik's solution.  If for some reason you are afraid of
logarithms, you could also do:

>>> x = 4978
>>> decades = [10 ** n for n in xrange(-1,8)]
>>> import itertools
>>> itertools.ifilter(lambda decade: x < decade, decades).next()
10000

BTW, are the python-dev guys aware that 10 ** -1 = 0.10000000000000001 ?




More information about the Python-list mailing list