Created
February 16, 2017 21:15
-
-
Save fxg42/a585947ccd787530455cb98e8344a39f to your computer and use it in GitHub Desktop.
Currency format
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
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