Skip to content

Commit

Permalink
Merge pull request #26 from LaunchPlatform/bugfix-25-incorrect-small-…
Browse files Browse the repository at this point in the history
…decimal-format

Fix #25 incorrect small decimal format issue
  • Loading branch information
fangpenlin authored Oct 9, 2024
2 parents 2545145 + 0868be0 commit 5fe09a2
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion beancount_black/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def format_number(self, token: Token) -> str:
raise ValueError("Expected a NUMBER")
value = token.value.replace(",", "")
number = decimal.Decimal(value)
return f"{number:,}"
return f"{number:,f}"

def format_number_atom(self, tree_or_token: typing.Union[Tree, Token]) -> str:
if isinstance(tree_or_token, Token):
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/expected_output/cost_and_price.bean
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
2022-04-01
Assets:Cash 20.0 USD @ 12,345.67 TWD
Assets:Cash 20.0 USD @@ 12,345.67 TWD
Assets:Cash 0.00000001 BTC {12,345 USD}
Assets:Cash 1.0 BTC {0.00000001 USD}
Assets:Cash 1.0 BTC {12,345 USD}
Assets:Cash 1.0 BTC {{12,345 USD}}
Assets:Cash 1.0 BTC {12,345 # 6,789 USD}
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/expected_output/number_expr.bean
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
2022-04-10 txn "payee" "Narration"
Assets:Bank 0.00000001 USD
Assets:Bank (12.34 + 56.78) USD
Assets:Bank (12.34 - 56.78) USD
Assets:Bank (12.34 * 56.78) USD
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/input/cost_and_price.bean
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
2022-04-01
Assets:Cash 20.0 USD @ 12345.67 TWD
Assets:Cash 20.0 USD @@ 12345.67 TWD
Assets:Cash 0.00000001 BTC {12345 USD}
Assets:Cash 1.0 BTC {0.00000001 USD}
Assets:Cash 1.0 BTC {12345 USD}
Assets:Cash 1.0 BTC {{12345 USD }}
Assets:Cash 1.0 BTC {12345 # 6789 USD}
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/input/number_expr.bean
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
2022-04-10 txn "payee" "Narration"
Assets:Bank 0.00000001 USD
Assets:Bank (12.34 + 56.78) USD
Assets:Bank ( 12.34 - 56.78 ) USD
Assets:Bank (12.34 * 56.78) USD
Expand Down
6 changes: 6 additions & 0 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def test_format_comment(formatter: Formatter, value: str, expected_result: str):
"+123.4567",
),
(Tree("number_expr", [Token("NUMBER", value="1234567.90")]), "1,234,567.90"),
(Tree("number_expr", [Token("NUMBER", value="0.00000001")]), "0.00000001"),
],
)
def test_format_number_expr(formatter: Formatter, tree: Tree, expected_result: str):
Expand Down Expand Up @@ -138,6 +139,11 @@ def test_format_number_expr(formatter: Formatter, tree: Tree, expected_result: s
"1970-01-01 balance Assets:Bank 12.34 ~ 0.015 USD",
"1970-01-01 balance Assets:Bank 12.34 ~ 0.015 USD",
),
(
dict(account_width=20, number_width=10),
"1970-01-01 balance Assets:Bank 0.00000001 USD",
"1970-01-01 balance Assets:Bank 0.00000001 USD",
),
],
)
def test_format_date_directive(
Expand Down

0 comments on commit 5fe09a2

Please sign in to comment.