From d5e065b51877928180350b5f70676647fc6d341b Mon Sep 17 00:00:00 2001 From: Samuel-21-eng Date: Fri, 7 Aug 2026 01:53:25 -0800 Subject: [PATCH] fix: rating display --- src/components/StarRating.test.tsx | 1 + src/components/StarRating.tsx | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/components/StarRating.test.tsx b/src/components/StarRating.test.tsx index 500002b..01a807e 100644 --- a/src/components/StarRating.test.tsx +++ b/src/components/StarRating.test.tsx @@ -15,6 +15,7 @@ describe("formatRating", () => { it("rounds half-up consistently", () => { expect(formatRating(4.25, 1)).toBe("4.3"); expect(formatRating(4.24, 1)).toBe("4.2"); + expect(formatRating(1.005, 2)).toBe("1.01"); }); it("supports a 0-decimal display", () => { diff --git a/src/components/StarRating.tsx b/src/components/StarRating.tsx index dadf999..2e99585 100644 --- a/src/components/StarRating.tsx +++ b/src/components/StarRating.tsx @@ -39,8 +39,10 @@ export type StarRatingProps = { export function formatRating(value: number, decimals = 1): string { const safe = Number.isFinite(value) ? value : 0; const clamped = Math.min(MAX_STARS, Math.max(0, safe)); - // toFixed already rounds half-up for positive numbers and pads decimals. - return clamped.toFixed(Math.max(0, decimals)); + const factor = Math.pow(10, decimals); + // Add a small epsilon to handle precision issues before rounding. + const rounded = Math.round((clamped + 1e-9) * factor) / factor; + return rounded.toFixed(Math.max(0, decimals)); } export default function StarRating({