Last active
December 4, 2018 19:24
-
-
Save dsimard/4448863 to your computer and use it in GitHub Desktop.
Fixed decimals
This file contains hidden or 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
(function fixed(float, decimal) { | |
decimal = decimal==undefined || decimal < 0 ? 2 : decimal; | |
return Math.round(float*Math.pow(10, decimal)).toString() | |
.replace((new RegExp("(\\d{"+decimal+"})$")), "$'.$1") | |
.replace(/^(-?)(\.)/, "$10.") | |
.replace(/^0$/, '0.00') | |
})(1.495) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment