Rounding up to the nearest exact logarithmic decade

Fredrik Lundh fredrik at pythonware.com
Tue Feb 28 17:41:37 EST 2006


Derek Basch wrote:

> Given a value (x) that is within the range (1e-1, 1e7) how do I round
> (x) up to the closest exact logarithmic decade? For instance:
>
> 10**3 = 1000
> x = 4978
> 10**4 = 10000
> x = 10000

how about

    >>> import math
    >>> def roundup(x):
    ...    return 10**math.ceil(math.log10(x))
    ...
    >>> roundup(4978)
    10000.0

?

</F>






More information about the Python-list mailing list