Skip to content

Instantly share code, notes, and snippets.

@estevan-ulian
Last active February 22, 2024 11:59
Show Gist options
  • Save estevan-ulian/792e9cbb02fe8b35b89616b1e04a68b6 to your computer and use it in GitHub Desktop.
Save estevan-ulian/792e9cbb02fe8b35b89616b1e04a68b6 to your computer and use it in GitHub Desktop.
Brazilian currency mask
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