Skip to content

Instantly share code, notes, and snippets.

@rodrigodiasnoronha
Last active March 11, 2024 12:47
Show Gist options
  • Select an option

  • Save rodrigodiasnoronha/e3f2aa71828e05c3a5261b46b69f9c33 to your computer and use it in GitHub Desktop.

Select an option

Save rodrigodiasnoronha/e3f2aa71828e05c3a5261b46b69f9c33 to your computer and use it in GitHub Desktop.
Currency Mask
const formatCurrency = (numberString = "") => {
let mask = numberString.replace(",", "").replace(".", "").replace(/\D/g, "");
const options = { minimumFractionDigits: 2 };
const result = new Intl.NumberFormat("pt-BR", options).format(parseFloat(mask) / 100);
if (result === "NaN") {
return "";
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment