Skip to content

Instantly share code, notes, and snippets.

@r
Created December 7, 2009 00:22
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 r/250509 to your computer and use it in GitHub Desktop.
Save r/250509 to your computer and use it in GitHub Desktop.
def significant_figures(num)
sn = num.to_s.sub(/e[-+]*\d+$/i, '') # remove the scientific notation
sn = sn.sub(/0*$/, '') unless sn.include?('.') # remove any trailing zeros
sn.sub('.', '').sub(/^[-+]?0*/, '').length # remove the sign, leading zeros
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment