Skip to content

Instantly share code, notes, and snippets.

@23maverick23
Last active October 12, 2015 00:17
Show Gist options
  • Save 23maverick23/3941954 to your computer and use it in GitHub Desktop.
Save 23maverick23/3941954 to your computer and use it in GitHub Desktop.
JavaScript: Round decimal
/*
* Simple script to correctly round up 2 decimal numbers.
* Helpful in handling currencies that use 2 decimal place.
* Will ensure 2 decimal places, even when ending in zeros.
* v = value to be rounded.
*/
function roundTwoDecimals (v) {
if (typeof(v) !== 'number') {
return alert('v must be a number!');
} else {
return (Math.round(v * Math.pow(10, 2)) / Math.pow(10, 2)).toFixed(2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment