Last active
December 14, 2015 01:58
-
-
Save daleobrien/5009766 to your computer and use it in GitHub Desktop.
Round a number by a number of significant digits
This file contains hidden or 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
from math import log10, floor | |
def round_sig(x, sig=2): | |
''' | |
>>> round_sig(0.0232) | |
0.023 | |
>>> round_sig(0.0232, 1) | |
0.02 | |
>>> round_sig(1234243, 3) | |
1230000.0 | |
''' | |
return round(x, sig - int(floor(log10(x))) - 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment