Skip to content

Commit

Permalink
Fix metric(0) crash.
Browse files Browse the repository at this point in the history
Call metrics(0) gives `ValueError: math domain error` before this patch.
  • Loading branch information
liukun committed Aug 8, 2022
1 parent dab62bf commit b1ee687
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 b1ee687

Please sign in to comment.