Skip to content

Instantly share code, notes, and snippets.

@dsimard
Last active December 4, 2018 19:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsimard/4448863 to your computer and use it in GitHub Desktop.
Save dsimard/4448863 to your computer and use it in GitHub Desktop.
Fixed decimals
(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