mirror of https://github.com/buster-so/buster.git
column labler formatter should handle decimals
This commit is contained in:
parent
ff6e672c16
commit
bdb65655b6
|
@ -18,6 +18,35 @@ describe('formatLabel', () => {
|
||||||
).toBe('1,234.567');
|
).toBe('1,234.567');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should format should pad the digits', () => {
|
||||||
|
expect(
|
||||||
|
formatLabel(1234, {
|
||||||
|
columnType: 'number',
|
||||||
|
style: 'number',
|
||||||
|
minimumFractionDigits: 2,
|
||||||
|
maximumFractionDigits: 3
|
||||||
|
})
|
||||||
|
).toBe('1,234.00');
|
||||||
|
|
||||||
|
expect(
|
||||||
|
formatLabel(1234.49, {
|
||||||
|
columnType: 'number',
|
||||||
|
style: 'number',
|
||||||
|
minimumFractionDigits: 4,
|
||||||
|
maximumFractionDigits: 4
|
||||||
|
})
|
||||||
|
).toBe('1,234.4900');
|
||||||
|
|
||||||
|
expect(
|
||||||
|
formatLabel(1234.49, {
|
||||||
|
columnType: 'number',
|
||||||
|
style: 'number',
|
||||||
|
minimumFractionDigits: 1,
|
||||||
|
maximumFractionDigits: 1
|
||||||
|
})
|
||||||
|
).toBe('1,234.5');
|
||||||
|
});
|
||||||
|
|
||||||
it('should format currency values', () => {
|
it('should format currency values', () => {
|
||||||
expect(
|
expect(
|
||||||
formatLabel(1234.56, {
|
formatLabel(1234.56, {
|
||||||
|
|
|
@ -72,6 +72,7 @@ export const formatLabel = (
|
||||||
) {
|
) {
|
||||||
const newNumber = Number(text) * multiplier;
|
const newNumber = Number(text) * multiplier;
|
||||||
const roundedNumber = roundNumber(newNumber, minimumFractionDigits, maximumFractionDigits);
|
const roundedNumber = roundNumber(newNumber, minimumFractionDigits, maximumFractionDigits);
|
||||||
|
|
||||||
if (style === 'currency') {
|
if (style === 'currency') {
|
||||||
formattedText = formatNumber(roundedNumber, {
|
formattedText = formatNumber(roundedNumber, {
|
||||||
currency,
|
currency,
|
||||||
|
@ -79,6 +80,7 @@ export const formatLabel = (
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
formattedText = formatNumber(roundedNumber, {
|
formattedText = formatNumber(roundedNumber, {
|
||||||
|
minDecimals: minimumFractionDigits,
|
||||||
minimumFractionDigits: Math.min(minimumFractionDigits, maximumFractionDigits),
|
minimumFractionDigits: Math.min(minimumFractionDigits, maximumFractionDigits),
|
||||||
maximumFractionDigits: Math.max(minimumFractionDigits, maximumFractionDigits),
|
maximumFractionDigits: Math.max(minimumFractionDigits, maximumFractionDigits),
|
||||||
useGrouping: numberSeparatorStyle !== null,
|
useGrouping: numberSeparatorStyle !== null,
|
||||||
|
|
|
@ -57,4 +57,15 @@ describe('formatNumber', () => {
|
||||||
'$3,363,690.40'
|
'$3,363,690.40'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('handles padding, should add zeros to the right', () => {
|
||||||
|
// formattedText = formatNumber(roundedNumber, {
|
||||||
|
// minimumFractionDigits: Math.min(minimumFractionDigits, maximumFractionDigits),
|
||||||
|
// maximumFractionDigits: Math.max(minimumFractionDigits, maximumFractionDigits),
|
||||||
|
// useGrouping: numberSeparatorStyle !== null,
|
||||||
|
// compact: compactNumbers
|
||||||
|
// });
|
||||||
|
|
||||||
|
expect(formatNumber(1234, { minDecimals: 2 })).toBe('1,234.00');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue