Skip to content

Instantly share code, notes, and snippets.

@fxg42
Created February 16, 2017 21:15
Show Gist options
  • Save fxg42/a585947ccd787530455cb98e8344a39f to your computer and use it in GitHub Desktop.
Save fxg42/a585947ccd787530455cb98e8344a39f to your computer and use it in GitHub Desktop.
Currency format
const reverseString = (s) =>
s.split('').reverse().join('')
export const currency = (amount = 0, currencyCode, lang) => {
try {
const decimalSep = lang === 'fr' ? ',' : '.'
const thousandSep = lang === 'fr' ? ' ' : ','
const [ integer, decimal ] = amount.toFixed(2).split('.')
const delim = reverseString(reverseString(integer).replace(/(\d{3})(?=\d)/g, `\$1${thousandSep}`))
if (lang === 'fr') {
return `${delim}${decimalSep}${decimal} ${currencyCode}`
} else {
return `${currencyCode} ${delim}${decimalSep}${decimal}`
}
} catch (err) {
return ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment