Skip to content

Instantly share code, notes, and snippets.

@palewire
Created November 15, 2011 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palewire/1368244 to your computer and use it in GitHub Desktop.
Save palewire/1368244 to your computer and use it in GitHub Desktop.
Rounding an integer to precision of "significant digits"
def roundint2precision(value, count):
"""
Accepts an integer and rounds it to the nearest number
at the specified precision, measured in "significant digits."
Example usage:
>> roundint2precision(1211111, 2)
1200000
>> roundint2precision(1211111, 3)
1210000
>> roundint2precision(1211111, 4)
1211000
>> roundint2precision(39, 1)
40
Sources:
http://en.wikipedia.org/wiki/Arithmetic_precision
"""
return int(round(value, (len(str(value)) - count) * -1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment