Last active
July 24, 2021 21:03
-
-
Save leodutra/cb0444255cba831a92226485bc7a0ad4 to your computer and use it in GitHub Desktop.
Format price using Intl.NumberFormat
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 formatPrice = ({ | |
price, | |
fraction = 2, | |
currency = 'USD', | |
currencyDisplay = 'symbol', // 'narrowSymbol' is not supported by Safari yet | |
locale = 'en-US', | |
signDisplay = 'auto', | |
...props | |
}) => { | |
if (isNaN(Number(price))) { | |
return price | |
} | |
return new Intl.NumberFormat(locale, { | |
style: 'currency', | |
currency, | |
currencyDisplay, | |
signDisplay, | |
minimumFractionDigits: fraction, | |
maximumFractionDigits: fraction, | |
...props | |
}).format(price) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment