Repository navigation
Round cart line totals to whole cents #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ export function cartTotal(items: { price: number; qty: number }[]): number { | |
| if (items[i].qty < 0) { | ||
| throw new RangeError(`Negative quantity for item ${i}`); | ||
| } | ||
| total += items[i].price * items[i].qty; | ||
| total += Math.round(items[i].price * items[i].qty); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unsafe floating-point line roundingMedium Severity
Reviewed by Cursor Bugbot for commit 4b9abd1. Configure here. |
||
| } | ||
| return total; | ||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The code rounds each line item's subtotal individually. Subtotals less than 0.5 will be rounded to 0, leading to an incorrect final cart total and potential under-charging.
Severity: MEDIUM
Suggested Fix
To prevent precision loss, sum the exact subtotals of all line items first, and then round the final total only once at the very end. For example:
return Math.round(items.reduce((acc, item) => acc + (item.price * item.qty), 0));. This ensures fractional values are not discarded prematurely.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.