Skip to content

Instantly share code, notes, and snippets.

@chnirt
Last active May 23, 2022 03:45
Show Gist options
  • Save chnirt/40d1b029de1d3ba2471f7a6d6db66b1f to your computer and use it in GitHub Desktop.
Save chnirt/40d1b029de1d3ba2471f7a6d6db66b1f to your computer and use it in GitHub Desktop.
format large number 9.939515029213079e+40
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