Skip to content

Commit

Permalink
Merge pull request #47 from liukun/fix-metric
Browse files Browse the repository at this point in the history
Fix "ValueError: math domain error" for metric(0)
  • Loading branch information
hugovk committed Aug 8, 2022
2 parents dab62bf + b1ee687 commit 14fc392
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/humanize/number.py
Expand Up @@ -508,7 +508,7 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str:
Returns:
str:
"""
exponent = int(math.floor(math.log10(abs(value))))
exponent = int(math.floor(math.log10(abs(value)))) if value != 0 else 0

if exponent >= 27 or exponent < -24:
return scientific(value, precision - 1) + unit
Expand Down
1 change: 1 addition & 0 deletions tests/test_number.py
Expand Up @@ -194,6 +194,7 @@ def test_clamp(test_args: list[typing.Any], expected: str) -> None:
@pytest.mark.parametrize(
"test_args, expected",
[
([0], "0.00"),
([1, "Hz"], "1.00 Hz"),
([1.0, "W"], "1.00 W"),
([3, "C"], "3.00 C"),
Expand Down

0 comments on commit 14fc392

Please sign in to comment.