We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
metric
2 parents 5cb0323 + e5a3976 commit 43d6dc6Copy full SHA for 43d6dc6
src/humanize/number.py
@@ -562,7 +562,7 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str:
562
ordinal_ = "mμnpfazyrq"[(-exponent - 1) // 3]
563
else:
564
ordinal_ = ""
565
- value_ = format(value, ".%if" % (precision - (exponent % 3) - 1))
+ value_ = format(value, ".%if" % max(0, precision - (exponent % 3) - 1))
566
if not (unit or ordinal_) or unit in ("°", "′", "″"):
567
space = ""
568
tests/test_number.py
@@ -269,6 +269,27 @@ def test_clamp(test_args: list[typing.Any], expected: str) -> None:
269
([0.1, "°"], "100m°"),
270
([100], "100"),
271
([0.1], "100 m"),
272
+ ([1.5123, "", 0], "2"),
273
+ ([10.5123, "", 0], "11"),
274
+ ([10.5123, "", 1], "11"),
275
+ ([10.5123, "", 2], "11"),
276
+ ([10.5123, "", 3], "10.5"),
277
+ ([1, "", 0], "1"),
278
+ ([10, "", 0], "10"),
279
+ ([100, "", 0], "100"),
280
+ ([1000, "", 0], "1 k"),
281
+ ([1, "", 1], "1"),
282
+ ([10, "", 1], "10"),
283
+ ([100, "", 1], "100"),
284
+ ([1000, "", 1], "1 k"),
285
+ ([1, "", 2], "1.0"),
286
+ ([10, "", 2], "10"),
287
+ ([100, "", 2], "100"),
288
+ ([1000, "", 2], "1.0 k"),
289
+ ([1, "", 3], "1.00"),
290
+ ([10, "", 3], "10.0"),
291
+ ([100, "", 3], "100"),
292
+ ([1000, "", 3], "1.00 k"),
293
([math.nan], "NaN"),
294
([math.nan, "m"], "NaN"),
295
([math.inf], "+Inf"),
0 commit comments