Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions src/utils/formatting.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import numeral from 'numeral';

const formatNumber = (amount, currency, options = {}) => {
if (!(options.maximumFractionDigits || options.maximumFractionDigits === 0)) {
if (
!(options.maximumFractionDigits || options.maximumFractionDigits === 0) &&
currency
) {
options.maximumFractionDigits = Math.max(
0,
7 - parseInt(amount, 10).toString().length
);
}

let value = new Intl.NumberFormat(navigator.language, {
style: 'decimal',
...options,
}).format(Math.abs(amount));
if (!currency || options.minPrecision) {
options.maximumFractionDigits = 2;
options.minimumFractionDigits = 2;
}

let value = new Intl.NumberFormat(navigator.language, options).format(
Math.abs(amount)
);

if (currency) {
value = `$${value}`;
Expand All @@ -22,17 +27,6 @@ const formatNumber = (amount, currency, options = {}) => {
value = `${direction}${value}`;
}

if (options.minPrecision) {
// Set min precision
value = value.replace(/\d+(?:,\d+)*(?:\.\d+)?/, match => {
const matchValue = parseFloat(match.replace(/,/g, ''));
if (matchValue >= 0.1 || options.directionSymbol) {
match = numeral(matchValue).format('0,0.00');
}
return match;
});
}

return value;
};

Expand Down