Skip to content

Instantly share code, notes, and snippets.

@kafeltz
Last active November 20, 2022 15:48
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 kafeltz/1b99cbaa9dc2142e04ca to your computer and use it in GitHub Desktop.
Save kafeltz/1b99cbaa9dc2142e04ca to your computer and use it in GitHub Desktop.
Solução brutalmente simples pra formatar moeda brasileira (BRL) de float pra string. Sem framework ou lib. Altere conforme a necessidade.
// http://blog.tompawlak.org/number-currency-formatting-javascript
// http://www.javascripter.net/faq/tofixed.htm
function _format(num)
{
return num.toFixed(2).replace(".", ",").replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.");
}
@kafeltz
Copy link
Author

kafeltz commented Aug 12, 2015

- 0.1     0,10
- 0.11    0,11
- 0.111   0,11
- 0.99    0,99
- 1       1,00
- 10      10,00
- 100     100,00
- 1000    1.000,00
- 10000   10.000,00
- 100000  100.000,00
- 1000000 1.000.000,00
- 99.99   99,99
- 99.999  100,00
- 99.01   99,01
- 99.09   99,09
- 99.199  99,20

https://jsfiddle.net/shalanga/vL9ktu9m/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment