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
2 changes: 2 additions & 0 deletions ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def to_cents(amount):
"""Turn a dollar amount (like 19.99 or '19.99') into integer cents."""
if amount is None or amount == "":
return 0
if isinstance(amount, str):
amount = amount.strip().replace(",", "")
return int(round(float(amount) * 100))


Expand Down
14 changes: 14 additions & 0 deletions tests/test_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ def test_format_money():
assert ledger.format_money(0) == "0.00"


def test_format_money_edge_cases():
assert ledger.format_money(1) == "0.01" # exactly one cent
assert ledger.format_money(-199) == "-1.99" # negative amount
assert ledger.format_money(100000001) == "1,000,000.01" # over a million


def test_to_cents_edge_cases():
assert ledger.to_cents(0.01) == 1 # exactly one cent
assert ledger.to_cents(-1.99) == -199 # negative amount
assert ledger.to_cents("1,234.56") == 123456 # comma thousands separator
assert ledger.to_cents("1,000,000.01") == 100000001 # over a million with commas
assert ledger.to_cents("") == 0


# -------------------------------------------------------------- accounts


Expand Down
Loading