Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
closes #374
Before Fix
$1.00 (100 cents) split among 3 people:
expense.amount = 100 // cents
remaining = 100
paidFors.length = 3 // Johannes, Janina, Jakob
// Person 1 (Johannes) - index 0, isLast = false
dividedAmount = (100 * 1) / 3 = 33.333...
remaining = 100 - 33.333... = 66.666...
Johannes.paidFor += 33.333...
// Person 2 (Janina) - index 1, isLast = false
dividedAmount = (100 * 1) / 3 = 33.333...
remaining = 66.666... - 33.333... = 33.333...
Janina.paidFor += 33.333...
// Person 3 (Jakob) - index 2, isLast = true
dividedAmount = remaining = 33.333...
remaining = 33.333... - 33.333... = 0
Jakob.paidFor += 33.333...
// Later rounding (lines 57-58)
Johannes.paidFor = Math.round(33.333...) = 33
Janina.paidFor = Math.round(33.333...) = 33
Jakob.paidFor = Math.round(33.333...) = 33
// RESULT: 33 + 33 + 33 = 99 cents
After Fix
// Initial state
expense.amount = 100 // cents
remaining = 100
paidFors.length = 3
// Person 1 (Johannes) - index 0, isLast = false
dividedAmount = Math.round((100 * 1) / 3) = Math.round(33.333...) = 33
remaining = 100 - 33 = 67
Johannes.paidFor += 33
// Person 2 (Janina) - index 1, isLast = false
dividedAmount = Math.round((100 * 1) / 3) = Math.round(33.333...) = 33
remaining = 67 - 33 = 34
Janina.paidFor += 33
// Person 3 (Jakob) - index 2, isLast = true
dividedAmount = remaining = 34 // Gets the leftover!
remaining = 34 - 34 = 0
Jakob.paidFor += 34
// No additional rounding needed since we already rounded during calculation
// RESULT: 33 + 33 + 34 = 100 cents