Last active
October 12, 2015 00:17
-
-
Save 23maverick23/3941954 to your computer and use it in GitHub Desktop.
JavaScript: Round decimal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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