# Rounding up to the nearest exact logarithmic decade

Discussion in 'Python' started by Derek Basch, Feb 28, 2006.

1. ### Derek BaschGuest

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

Thanks Everyone!
Derek Basch

Derek Basch, Feb 28, 2006

2. ### Fredrik LundhGuest

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

>>> import math
>>> def roundup(x):

... return 10**math.ceil(math.log10(x))
...
>>> roundup(4978)

10000.0

?

</F>

Fredrik Lundh, Feb 28, 2006

3. ### Guest

Quoting Derek Basch <>:

> 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:

def roundup(x):
if x < 1:
return 1
else:
return '1' + ('0' * len(str(int(x))))

Jack Orenstein

, Feb 28, 2006
4. ### Felipe Almeida LessaGuest

Em Ter, 2006-02-28 Ã s 17:47 -0500, escreveu:
> Quoting Derek Basch <>:
>
> > 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:

>
>
> def roundup(x):
> if x < 1:
> return 1
> else:
> return '1' + ('0' * len(str(int(x))))

No dice. First, it returns an int for some cases and a string for the
others. Second, casting from str to int and vice-versa and concatenating
strings won't perform any good. I wouldn't like this hack on my code.

My 2Â¢,
Felipe.

--
"Quem excele em empregar a forÃ§a militar subjulga os exÃ©rcitos dos
povos sem as atacar e destrÃ³i os estados dos outros povos sem lutas
'preservaÃ§Ã£o'. Desse modo suas armas nÃ£o se embotarÃ£o, e os ganhos

-- Sun Tzu, em "A arte da guerra"

Felipe Almeida Lessa, Feb 28, 2006
5. ### Derek BaschGuest

Thanks effbot. I knew their had to be something buried in the math
module that could help. ceil() it is!

</dTb>

Derek Basch, Feb 28, 2006
6. ### Grant EdwardsGuest

On 2006-02-28, <> wrote:
> Quoting Derek Basch <>:
>
>> 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:

>
>
> def roundup(x):
> if x < 1:
> return 1
> else:
> return '1' + ('0' * len(str(int(x))))

Interesting approach. You're converting to string at the same
time. I would guess the OP wanted a float.

--
Grant Edwards grante Yow! It's OKAY -- I'm an
at INTELLECTUAL, too.
visi.com

Grant Edwards, Feb 28, 2006
7. ### Guest

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

10000

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

, Feb 28, 2006
8. ### Grant EdwardsGuest

On 2006-02-28, <> wrote:
> 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

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

You're joking, right?

--
Grant Edwards grante Yow! HOORAY, Ronald!! Now
at YOU can marry LINDA