Created
November 15, 2011 20:31
-
-
Save palewire/1368244 to your computer and use it in GitHub Desktop.
Rounding an integer to precision of "significant digits"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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