Monday
18Aug2008
Seriously brain-dead way to round off numbers in Python
Monday, August 18, 2008 at 5:09PM import decimal
def Round(value, precision = 3):
return float(str(
decimal.Decimal(str(value)).quantize(decimal.Decimal("1")
/ (decimal.Decimal('1' + '0' * precision)), decimal.ROUND_HALF_UP)))






Reader Comments (1)
I guess I should mention that if you really wanted to round off to 3 decimal places, you could just do.
import math
def Round(value, precision = 3):
return int(value * math.pow(10, precision)) / math.pow(10, precision)