Skip to content

Instantly share code, notes, and snippets.

@tiagofrancafernandes
Created September 4, 2024 18:55
Show Gist options
  • Save tiagofrancafernandes/6776bc9df5cb8e6b6f6d58f631f80353 to your computer and use it in GitHub Desktop.
Save tiagofrancafernandes/6776bc9df5cb8e6b6f6d58f631f80353 to your computer and use it in GitHub Desktop.
JS currency and number snippets
function currencyFormat(value, currency = 'USD', locale = 'en-US') {
return new Intl.NumberFormat(
locale, { style: 'currency', currency }).format(value);
}
currencyFormat(15); // $15.00
currencyFormat(15, 'USD', 'en-US'); // $15.00
currencyFormat(15, 'BRL', 'en-US'); // R$15.00
currencyFormat(15, 'USD', 'pt-BR'); // US$ 15,00
currencyFormat(15, 'BRL', 'pt-BR'); // R$ 15,00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment