From 11e503b7d601b55f9bdd64a092fcec36048c006d Mon Sep 17 00:00:00 2001 From: Yxq Date: Tue, 20 Oct 2026 16:26:14 +0800 Subject: [PATCH] fix: honour a zero decimal count in roundedNum ## Motivation ```ts export const roundedNum = (num: number, decimalPoint?: number) => { const base = Math.pow(10, decimalPoint || 2); return Math.round(num * base) / base; }; ``` `|| 2` treats an explicit `0` as "not provided", so the one value that means "round to whole numbers" silently behaves like `2`: ```js roundedNum(1.9, 0) // 1.9 - expected 2 ``` No caller passes `0` today (`shortenAxisLabel` uses `1` and `convertMonthsToDays` omits it), so this is a latent defect in an exported helper rather than a live rendering bug - but the documented parameter silently ignores a legal value. ## Change `decimalPoint ?? 2`, so only `undefined` falls back to the default. ## Verification ``` # test only Tests: 1 failed, 3 passed, 4 total # with the fix Tests: 4 passed, 4 total ``` Signed-off-by: Yxq --- apps/web/src/common/utils/format.test.tsx | 5 +++++ apps/web/src/common/utils/format.tsx | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/apps/web/src/common/utils/format.test.tsx b/apps/web/src/common/utils/format.test.tsx index 7b6f1ea559..622a3c15c5 100644 --- a/apps/web/src/common/utils/format.test.tsx +++ b/apps/web/src/common/utils/format.test.tsx @@ -32,4 +32,9 @@ describe('format', () => { expect(roundedNum(1.2345, 3)).toEqual(1.235); expect(roundedNum(100000)).toEqual(100000); }); + + it('honours a zero decimal count instead of falling back to two', function () { + expect(roundedNum(1.9, 0)).toEqual(2); + expect(roundedNum(1.4, 0)).toEqual(1); + }); }); diff --git a/apps/web/src/common/utils/format.tsx b/apps/web/src/common/utils/format.tsx index b684d448ba..13f29d048a 100644 --- a/apps/web/src/common/utils/format.tsx +++ b/apps/web/src/common/utils/format.tsx @@ -124,7 +124,9 @@ export const fmtEmptyDataValue = (value: any): any => { }; export const roundedNum = (num: number, decimalPoint?: number) => { - const base = Math.pow(10, decimalPoint || 2); + // `|| 2` turned an explicit 0 into two decimals, so rounding to whole + // numbers was impossible. + const base = Math.pow(10, decimalPoint ?? 2); return Math.round(num * base) / base; };