Skip to content

Commit

Permalink
metric: Fix precision format (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Dec 9, 2023
2 parents 5cb0323 + e5a3976 commit 43d6dc6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str:
ordinal_ = "mμnpfazyrq"[(-exponent - 1) // 3]
else:
ordinal_ = ""
value_ = format(value, ".%if" % (precision - (exponent % 3) - 1))
value_ = format(value, ".%if" % max(0, precision - (exponent % 3) - 1))
if not (unit or ordinal_) or unit in ("°", "′", "″"):
space = ""
else:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,27 @@ def test_clamp(test_args: list[typing.Any], expected: str) -> None:
([0.1, "°"], "100m°"),
([100], "100"),
([0.1], "100 m"),
([1.5123, "", 0], "2"),
([10.5123, "", 0], "11"),
([10.5123, "", 1], "11"),
([10.5123, "", 2], "11"),
([10.5123, "", 3], "10.5"),
([1, "", 0], "1"),
([10, "", 0], "10"),
([100, "", 0], "100"),
([1000, "", 0], "1 k"),
([1, "", 1], "1"),
([10, "", 1], "10"),
([100, "", 1], "100"),
([1000, "", 1], "1 k"),
([1, "", 2], "1.0"),
([10, "", 2], "10"),
([100, "", 2], "100"),
([1000, "", 2], "1.0 k"),
([1, "", 3], "1.00"),
([10, "", 3], "10.0"),
([100, "", 3], "100"),
([1000, "", 3], "1.00 k"),
([math.nan], "NaN"),
([math.nan, "m"], "NaN"),
([math.inf], "+Inf"),
Expand Down

0 comments on commit 43d6dc6

Please sign in to comment.