Last active
May 23, 2022 03:45
-
-
Save chnirt/40d1b029de1d3ba2471f7a6d6db66b1f to your computer and use it in GitHub Desktop.
format large number 9.939515029213079e+40
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
import numeral from 'numeral'; | |
export const formatCurrency = (number, symbol = '$') => { | |
const formatScientificNotationNumber = Number(number).toFixed(8); // format for case number = 9.322320693172514e-12 | |
if (formatScientificNotationNumber.includes('e+')) { | |
var formatter = new Intl.NumberFormat('en-AU', { | |
style: 'currency', | |
currency: 'AUD', | |
minimumFractionDigits: 0, | |
maximumFractionDigits: 0, | |
}); | |
return formatter.format(number); | |
} | |
const currencyPattern = | |
symbol + | |
(numeral(formatScientificNotationNumber)._value > 1000000 || | |
numeral(formatScientificNotationNumber)._value < -1000000 | |
? '0,0' | |
: '0,0[.]00'); | |
return numeral(formatScientificNotationNumber).format(currencyPattern); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment