Skip to content

Instantly share code, notes, and snippets.

@marco-souza
Created July 2, 2018 21:19
Show Gist options
  • Select an option

  • Save marco-souza/b1bc2314265f25c60884deb8fea00e7a to your computer and use it in GitHub Desktop.

Select an option

Save marco-souza/b1bc2314265f25c60884deb8fea00e7a to your computer and use it in GitHub Desktop.
Format a simple number to Reais
function formatMoney(n) {
return "R$ " + n.toFixed(2).replace('.', ',').replace(/(\d)(?=(\d{3})+\,)/g, "$1.");
}
// or (ES6)
const formatMoney = n => `R$ ${n.toFixed(2).replace('.', ',').replace(/(\d)(?=(\d{3})+\,)/g, "$1.")}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment