Skip to content

Instantly share code, notes, and snippets.

@atesgoral
Created July 6, 2009 19:02
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 atesgoral/141614 to your computer and use it in GitHub Desktop.
Save atesgoral/141614 to your computer and use it in GitHub Desktop.
function sigFigs(n, sig) {
var mult = Math.pow(10,
sig - Math.floor(Math.log(n) / Math.LN10) - 1);
return Math.round(n * mult) / mult;
}
alert(sigFigs(1234567, 3)); // Gives 1230000
alert(sigFigs(0.06805, 3)); // Gives 0.0681
alert(sigFigs(5, 3)); // Gives 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment