2
def round_up(value, multiple):
return multiple * math.ceil(float(value) / multiple)
def digits(value):
return int(math.log(value, 10)) + 1
def round_price(value):
if value < 10000:
return int(round_up(value, 1000))
d = digits(value)
new_value = int(round_up(value, 10 ** (d - 2)))
new_value -= 10 ** (d - 3)
return new_value
5
import math
roundup = lambda x: math.ceil(x/1000.0) * 1000
Although rounding 353990 to 359000 makes no sense. This function will round 353990 up to 354000.
If you want normal rounding rather than a ’round up’, you would just use the builtin funciton round
:
round(x, -3)
and so for a generic roundup
with the same function signature as round
def roundup(x, n=0):
return math.ceil(x * (10**n)) * (10**-n)
Source:stackexchange.com