Skip to content
Open
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
1 change: 1 addition & 0 deletions src/components/StarRating.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
6 changes: 4 additions & 2 deletions src/components/StarRating.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down