Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active Jul 24, 2021
Embed
What would you like to do?
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