Last active
December 10, 2019 20:49
-
-
Save JulianeAlbuquerque/d0143835fdeada0f350bc9c16a517b7b to your computer and use it in GitHub Desktop.
This file contains 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
// 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Muito bom!