Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active July 24, 2021 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leodutra/cb0444255cba831a92226485bc7a0ad4 to your computer and use it in GitHub Desktop.
Save leodutra/cb0444255cba831a92226485bc7a0ad4 to your computer and use it in GitHub Desktop.
Format price using Intl.NumberFormat
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