column labler formatter should handle decimals

This commit is contained in:
Nate Kelley 2025-04-11 16:01:16 -06:00
parent ff6e672c16
commit bdb65655b6
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
3 changed files with 42 additions and 0 deletions

View File

@ -18,6 +18,35 @@ describe('formatLabel', () => {
).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', () => {
expect(
formatLabel(1234.56, {

View File

@ -72,6 +72,7 @@ export const formatLabel = (
) {
const newNumber = Number(text) * multiplier;
const roundedNumber = roundNumber(newNumber, minimumFractionDigits, maximumFractionDigits);
if (style === 'currency') {
formattedText = formatNumber(roundedNumber, {
currency,
@ -79,6 +80,7 @@ export const formatLabel = (
});
} else {
formattedText = formatNumber(roundedNumber, {
minDecimals: minimumFractionDigits,
minimumFractionDigits: Math.min(minimumFractionDigits, maximumFractionDigits),
maximumFractionDigits: Math.max(minimumFractionDigits, maximumFractionDigits),
useGrouping: numberSeparatorStyle !== null,

View File

@ -57,4 +57,15 @@ describe('formatNumber', () => {
'$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');
});
});