Skip to content

Instantly share code, notes, and snippets.

@sisiwei
Created October 26, 2012 18:11
Show Gist options
  • Select an option

  • Save sisiwei/3960393 to your computer and use it in GitHub Desktop.

Select an option

Save sisiwei/3960393 to your computer and use it in GitHub Desktop.
Round any number and return with or without label
/* PARAMS:
number = the raw number, it can be a string
decimaloutput = the number of decimal points you want it to return
label = optional param, the label you want returned at the end like 'thousand'
*/
function roundNumber(number, decimaloutput, label){
var n = parseInt(number),
s = n.toString(),
c = s.length - 1,
d = Math.pow(10, c),
f = n/d;
var output = (label == undefined) ? f.toFixed(decimaloutput) : f.toFixed(decimaloutput) + ' ' + label;
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment