Created
September 4, 2024 18:55
-
-
Save tiagofrancafernandes/6776bc9df5cb8e6b6f6d58f631f80353 to your computer and use it in GitHub Desktop.
JS currency and number snippets
This file contains hidden or 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
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