diff options
author | xengineering <me@xengineering.eu> | 2024-09-04 20:16:15 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-09-08 17:21:32 +0200 |
commit | b85de39cd765164d5a31836cb7ca106ad6e328a7 (patch) | |
tree | af883c0203f564b0842b18797cc69007632a7fba /finance | |
parent | 4f3d4de3796744ee1d6d0e91e8bec76e0487de90 (diff) | |
download | finance-py-b85de39cd765164d5a31836cb7ca106ad6e328a7.tar finance-py-b85de39cd765164d5a31836cb7ca106ad6e328a7.tar.zst finance-py-b85de39cd765164d5a31836cb7ca106ad6e328a7.zip |
Switch to decimal.Decimal for money
Floating point data types are not suitable to represent money since
rounding issues will sum up.
Diffstat (limited to 'finance')
-rw-r--r-- | finance/income.py | 7 | ||||
-rw-r--r-- | finance/test_income.py | 11 |
2 files changed, 10 insertions, 8 deletions
diff --git a/finance/income.py b/finance/income.py index 94759ff..8d775cb 100644 --- a/finance/income.py +++ b/finance/income.py @@ -1,17 +1,18 @@ import dataclasses from datetime import datetime +from decimal import Decimal @dataclasses.dataclass(kw_only=True, frozen=True) class Income: """Income models financial income paid on the first day of a month""" - amount: float + amount: Decimal - def integrate(self, start: datetime, end: datetime) -> float: + def integrate(self, start: datetime, end: datetime) -> Decimal: """Integrate the income from a start to an end date""" - retval = 0.0 + retval = Decimal(0.0) current = datetime(start.year, start.month, 1) diff --git a/finance/test_income.py b/finance/test_income.py index 0ce3507..fc75bef 100644 --- a/finance/test_income.py +++ b/finance/test_income.py @@ -1,16 +1,17 @@ from datetime import datetime +from decimal import Decimal from finance import income def test_income_integration() -> None: - inc = income.Income(amount=3000.0) + inc = income.Income(amount=Decimal(3000.0)) tests = ( - (datetime(2024, 3, 12), datetime(2024, 4, 2), 3000.0), - (datetime(2024, 3, 1), datetime(2024, 3, 15), 3000.0), - (datetime(2024, 2, 25), datetime(2024, 3, 1), 3000.0), - (datetime(2024, 2, 25), datetime(2024, 6, 12), 12000.0), + (datetime(2024, 3, 12), datetime(2024, 4, 2), Decimal(3000.0)), + (datetime(2024, 3, 1), datetime(2024, 3, 15), Decimal(3000.0)), + (datetime(2024, 2, 25), datetime(2024, 3, 1), Decimal(3000.0)), + (datetime(2024, 2, 25), datetime(2024, 6, 12), Decimal(12000.0)), ) for test in tests: |