Skip to content
Closed
Show file tree
Hide file tree
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
32 changes: 31 additions & 1 deletion src/number.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { firstDecimalDigit, round } from './number';
import { firstDecimalDigit, formatDecimal, round } from './number';

describe('round', () => {
it.each([
Expand Down Expand Up @@ -26,3 +26,33 @@ describe('firstDecimalDigit', () => {
expect(firstDecimalDigit(number)).toEqual(expectedResult);
});
});

describe('formatDecimal', () => {
it.each([
[
1.2345,
{ maxDecimals: 2, minDecimals: 1, trimTrailingZeros: true },
'1.23',
],
[
1.2,
{ maxDecimals: 3, minDecimals: 3, trimTrailingZeros: false },
'1.200',
],
[
1.2345,
{ maxDecimals: 4, minDecimals: 2, trimTrailingZeros: false },
'1.2345',
],
[1.2, { maxDecimals: 4, minDecimals: 2, trimTrailingZeros: true }, '1.2'],
[1, { maxDecimals: 2, minDecimals: 2, trimTrailingZeros: false }, '1.00'],
[
-1.234,
{ maxDecimals: 2, minDecimals: 0, trimTrailingZeros: true },
'-1.23',
],
[0, { maxDecimals: 3, minDecimals: 0, trimTrailingZeros: true }, '0'],
])('formatDecimal(%p, %p) → %p', (value, options, expected) => {
expect(formatDecimal(value, options)).toEqual(expected);
});
});
22 changes: 22 additions & 0 deletions src/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,25 @@ export function firstDecimalDigit(number: number): number {

return Number(regExpMatchArray[1]);
}

export function formatDecimal(
value: number,
options: {
maxDecimals: number;
minDecimals: number;
trimTrailingZeros: boolean;
},
): string {
const { maxDecimals, minDecimals, trimTrailingZeros } = options;

const factor = 10 ** maxDecimals;
const rounded = Math.round(value * factor) / factor;
Comment on lines +36 to +37
Copy link

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rounding logic duplicates the existing round function. Consider using round(value, maxDecimals) instead to avoid code duplication and maintain consistency.

Suggested change
const factor = 10 ** maxDecimals;
const rounded = Math.round(value * factor) / factor;
const rounded = round(value, maxDecimals);

Copilot uses AI. Check for mistakes.

let str = rounded.toFixed(Math.max(minDecimals, maxDecimals));

if (trimTrailingZeros) {
str = str.replace(/(\.[0-9]*?)0+$/, '$1').replace(/\.$/, '');
Comment on lines +39 to +42
Copy link

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for toFixed should use maxDecimals to ensure proper rounding, then handle minDecimals separately. Currently, when minDecimals > maxDecimals, this will display more decimals than the rounded value supports, potentially showing incorrect precision.

Suggested change
let str = rounded.toFixed(Math.max(minDecimals, maxDecimals));
if (trimTrailingZeros) {
str = str.replace(/(\.[0-9]*?)0+$/, '$1').replace(/\.$/, '');
// Always round to maxDecimals
let str = rounded.toFixed(maxDecimals);
if (trimTrailingZeros) {
// Remove trailing zeros, but ensure at least minDecimals remain
const [intPart, decPart = ''] = str.split('.');
let trimmedDec = decPart.replace(/0+$/, '');
// Pad with zeros if needed to reach minDecimals, but never more than maxDecimals
if (trimmedDec.length < minDecimals) {
trimmedDec = trimmedDec.padEnd(minDecimals, '0');
}
// If there are decimals, join; else, just intPart
str = trimmedDec.length > 0 ? `${intPart}.${trimmedDec}` : intPart;
} else if (minDecimals < maxDecimals) {
// If not trimming, pad with zeros to minDecimals if needed
const [intPart, decPart = ''] = str.split('.');
let paddedDec = decPart;
if (paddedDec.length < minDecimals) {
paddedDec = paddedDec.padEnd(minDecimals, '0');
}
str = paddedDec.length > 0 ? `${intPart}.${paddedDec}` : intPart;

Copilot uses AI. Check for mistakes.
}

return str;
}