Latest Comments
Search
« Yin and Yang | Main | Computer Go Advances »

Seriously brain-dead way to round off numbers in Python

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)

July 9, 2009 | Unregistered Commenteryj

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>