diff --git a/conservancy_beancount/reports/core.py b/conservancy_beancount/reports/core.py index 6b2845c..6b759d8 100644 --- a/conservancy_beancount/reports/core.py +++ b/conservancy_beancount/reports/core.py @@ -198,17 +198,18 @@ class Balance(Mapping[str, data.Amount]): fmt: Optional[str]='#,#00.00 ¤¤', sep: str=', ', empty: str="Zero balance", + tolerance: Optional[Decimal]=None, ) -> str: """Formats the balance as a string with the given parameters - If the balance is zero, returns ``empty``. Otherwise, returns a string - with each amount in the balance formatted as ``fmt``, separated by - ``sep``. + If the balance is zero (within tolerance), returns ``empty``. + Otherwise, returns a string with each amount in the balance formatted + as ``fmt``, separated by ``sep``. If you set ``fmt`` to None, amounts will be formatted according to the user's locale. The default format is Beancount's input format. """ - amounts = [amount for amount in self.values() if amount.number] + amounts = list(self.clean_copy(tolerance).values()) if not amounts: return empty amounts.sort(key=lambda amt: abs(amt.number), reverse=True) diff --git a/tests/test_reports_balance.py b/tests/test_reports_balance.py index 706531e..be5b265 100644 --- a/tests/test_reports_balance.py +++ b/tests/test_reports_balance.py @@ -32,6 +32,8 @@ DEFAULT_STRINGS = [ ({'USD': '20.00'}, "20.00 USD"), ({'EUR': '50.00', 'GBP': '80.00'}, "80.00 GBP, 50.00 EUR"), ({'JPY': '-5500.00', 'BRL': '-8500.00'}, "-8,500.00 BRL, -5,500 JPY"), + ({'USD': 10, 'EUR': '.00015'}, "10.00 USD"), + ({'JPY': '-.00015'}, "Zero balance"), ] TOLERANCES = [Decimal(n) for n in ['.1', '.01', '.001', 0]] @@ -379,3 +381,21 @@ def test_format_none(): def test_format_empty(empty): balance = core.Balance() assert balance.format(empty=empty) == empty + +@pytest.mark.parametrize('tolerance', TOLERANCES) +def test_str_tolerance(tolerance): + chf = testutil.Amount('.005', 'CHF') + actual = str(core.Balance([chf], tolerance)) + if tolerance > chf.number: + assert actual == "Zero balance" + else: + assert actual == "00.00 CHF" + +@pytest.mark.parametrize('tolerance', TOLERANCES) +def test_format_tolerance(tolerance): + chf = testutil.Amount('.005', 'CHF') + actual = core.Balance([chf]).format(tolerance=tolerance) + if tolerance > chf.number: + assert actual == "Zero balance" + else: + assert actual == "00.00 CHF"