Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metric: Fix precision format #159

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading