Skip to content

Instantly share code, notes, and snippets.

@thengineer
Last active August 29, 2015 13:59
Show Gist options
  • Save thengineer/10956622 to your computer and use it in GitHub Desktop.
Save thengineer/10956622 to your computer and use it in GitHub Desktop.
round a float to a defined number of significant digits
def round_significant(number, nsig):
from math import log10
if number != 0:
lg10 = int((log10(abs(number))))
if abs(number) < 1:
lg10 -= 1
ndig = nsig - lg10 - 1
rded = round(number, ndig)
if ndig <= 0:
rded = int(rded)
return rded
else:
return int(number)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment