Skip to content
Merged
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
9 changes: 8 additions & 1 deletion src/bill/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,14 @@ def get_shares_spreadsheet(self):
current_row += 1

for extra in self.extras.items:
row_data = [extra.name, extra.price]
percentage = (extra.price / subtotal) * 100
Comment thread
dphoria marked this conversation as resolved.
if percentage == int(percentage):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Percentage Formatting Fails Due to Precision

The direct floating-point equality check for whole number percentages can fail due to precision issues. This results in percentages that are effectively whole numbers being incorrectly formatted with decimals (e.g., "20.00%") instead of as clean whole numbers (e.g., "20%").

Fix in Cursor Fix in Web

formatted_percentage = f"{int(percentage)}%"
else:
formatted_percentage = f"{percentage:.2f}%"
extra_name_with_percentage = f"{extra.name} ({formatted_percentage})"

row_data = [extra_name_with_percentage, extra.price]
for col_idx in range(person_count):
col_letter = chr(ord("C") + col_idx)
formula = (
Expand Down
4 changes: 2 additions & 2 deletions tests/test_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def test_spreadsheet(calculator, sample_persons):
assert worksheet[f"F{subtotal_row}"].value == "=SUM(C18:E18)"

service_charge_row = 19
assert worksheet[f"A{service_charge_row}"].value == SERVICE_CHARGE
assert worksheet[f"A{service_charge_row}"].value == "Service Charge (20%)"
service_charge = subtotal * SERVICE_CHARGE_RATIO
assert worksheet[f"B{service_charge_row}"].value == service_charge

Expand All @@ -289,7 +289,7 @@ def test_spreadsheet(calculator, sample_persons):
)

tax_row = 20
assert worksheet[f"A{tax_row}"].value == TAX
assert worksheet[f"A{tax_row}"].value == "Tax (10.35%)"
tax = subtotal * TAX_RATIO
assert worksheet[f"B{tax_row}"].value == tax

Expand Down