diff --git a/web/src/lib/columnFormatter.test.ts b/web/src/lib/columnFormatter.test.ts index 8ed6dc8b0..e45703fbd 100644 --- a/web/src/lib/columnFormatter.test.ts +++ b/web/src/lib/columnFormatter.test.ts @@ -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, { diff --git a/web/src/lib/columnFormatter.ts b/web/src/lib/columnFormatter.ts index bb1798196..fbb29c7de 100644 --- a/web/src/lib/columnFormatter.ts +++ b/web/src/lib/columnFormatter.ts @@ -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, diff --git a/web/src/lib/numbers.test.ts b/web/src/lib/numbers.test.ts index 4cfe50bb6..caff9f370 100644 --- a/web/src/lib/numbers.test.ts +++ b/web/src/lib/numbers.test.ts @@ -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'); + }); });