Skip to content

Instantly share code, notes, and snippets.

@futuraprime
Created July 20, 2011 18:58
Show Gist options
  • Save futuraprime/1095648 to your computer and use it in GitHub Desktop.
Save futuraprime/1095648 to your computer and use it in GitHub Desktop.
/* sigdig
*
* rounds the number to a specified number of significant digits
*/
['sigdig', false, function(setting, data) {
if (typeof data[1] == "number") {
var num_store = data[1],
order = Math.ceil(Math.log( num_store )/Math.log(10)), // get order of magnitude from log10
n_down = num_store/Math.pow(10, order); // drop down
// now round, as above
n_down = n_down * Math.pow( 10, setting ); // bring the decimal down for rounding
n_down = Math.round(n_down);
n_down = n_down / Math.pow( 10, setting ); // and back.
//finally, undo the original transform
data[1] = n_down * Math.pow( 10, order );
}
return data;
}], //sigdig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment