I have a bunch of floats and I want to round them up to the next highest multiple of 10.
For example:
10.2 should be 20
10.0 should be 10
16.7 should be 20
94.9 should be 100
I only need it to go from the range 0-100. I tried math.ceil() but that only roun开发者_StackOverflow社区ds up to the nearest integer.
Thanks in advance.
from math import ceil
def ceil_to_tens(x):
return int(ceil(x / 10.0)) * 10
Edit: okay, now that I have an undeserved "Nice answer" badge for this answer, I think owe the community with a proper solution using the decimal
module that does not suffer from these problems :) Thanks to Jeff for pointing this out. So, a solution using decimal
works as follows:
from decimal import Decimal, ROUND_UP
def ceil_to_tens_decimal(x):
return (Decimal(x) / 10).quantize(1, rounding=ROUND_UP) * 10
Of course the above code requires x
to be an integer, a string or a Decimal
object - floats won't work as that would defeat the whole purpose of using the decimal
module.
It's a pity that Decimal.quantize
does not work properly with numbers larger than 1, it would have saved the division-multiplication trick.
>>> x = 16.7
>>> int( 10 * math.ceil(x/10.0))
The answers here are fraught with peril. For example 11*1.1 - 2.1 = 10.0
, right? But wait:
>>> x = 11*1.1 - 2.1
>>> int(ceil(x / 10.0)) * 10
20
>>> x
10.000000000000002
>>>
You could try this
int(ceil(round(x, 12) / 10.0)) * 10
But choosing the number of decimal places to round to is really difficult as it is hard to predict how floating point noise accumulates. If it is really important to get this right all of the time, then you need to use fixed point arithmetic or Decimal.
If you're looking for another solution that doesn't involve float division, here's one that uses the modulus:
def ceil_to_tens_mod(x):
tmp = int(ceil(x))
mod10 = tmp % 10
return tmp - mod10 + (10 if mod10 else 0)
There's probably some way to simplify it, but there you go.
精彩评论