Skip to content

Instantly share code, notes, and snippets.

@JulianeAlbuquerque
Last active December 10, 2019 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JulianeAlbuquerque/d0143835fdeada0f350bc9c16a517b7b to your computer and use it in GitHub Desktop.
Save JulianeAlbuquerque/d0143835fdeada0f350bc9c16a517b7b to your computer and use it in GitHub Desktop.
// https://blog.tompawlak.org/number-currency-formatting-javascript
function formatNumber (num) {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
}
console.info(formatNumber(2665)); // 2,665
console.info(formatNumber(102665)); // 102,665
console.info(formatNumber(111102665)); // 111,102,665
// Currency Formatting
function currencyFormat (num) {
return "$" + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
}
console.info(currencyFormat(2665)); // $2,665.00
console.info(currencyFormat(102665)); // $102,665.00
// ------------
function currencyFormatDE (num) {
return num
.toFixed(2) // always two decimal digits
.replace(".", ",") // replace decimal point character with ,
.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.") + " €" // use . as a separator
}
console.info(currencyFormatDE(1234567.89)); // output 1.234.567,89 €
// -----------
var currency = "R$ 1.100,77";
var currency = currency.replace(".", "").replace(",", ".")currency.replace("R$ ", "");
var currency = parseFloat(currency)
@khaueviana
Copy link

Muito bom!

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