Skip to content

Instantly share code, notes, and snippets.

@daleobrien
Last active December 14, 2015 01:58
Show Gist options
  • Save daleobrien/5009766 to your computer and use it in GitHub Desktop.
Save daleobrien/5009766 to your computer and use it in GitHub Desktop.
Round a number by a number of significant digits
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