Last active
February 22, 2024 11:59
-
-
Save estevan-ulian/792e9cbb02fe8b35b89616b1e04a68b6 to your computer and use it in GitHub Desktop.
Brazilian currency mask
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
const brlCurrencyMask = (e) => { | |
const { value } = e.target; | |
let mask = ""; | |
mask = value.replace(",", "").replace(".", "").replace(/\D/g, ""); | |
const options = { minimumFractionDigits: 2 }; | |
const result = new Intl.NumberFormat("pt-BR", options).format( | |
parseFloat(mask) / 100, | |
); | |
if (result === "NaN") { | |
e.target.value = ""; | |
return; | |
} | |
e.target.value = result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment