From 3690ec5a31b3cd4e2d3374198cf19f7023d3d72d Mon Sep 17 00:00:00 2001 From: agammann <161159040+agammann@users.noreply.github.com> Date: Sun, 20 Sep 2026 14:02:05 -0700 Subject: [PATCH] Fix significant figures when metric rounding carries within quetta --- src/humanize/number.py | 2 +- tests/test_number.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 52a5356a..eeb1ae13 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -582,7 +582,7 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str: value /= 10**old_bucket digits = int(max(0, precision - exponent % 3 - 1)) # Absorb a rounding carry (9.999 -> 10.0) by bumping the exponent. - if exponent < 30 and round(abs(value), digits) >= 10 ** (exponent % 3 + 1): + if exponent < 32 and round(abs(value), digits) >= 10 ** (exponent % 3 + 1): exponent += 1 new_bucket = exponent // 3 * 3 value /= 10 ** (new_bucket - old_bucket) diff --git a/tests/test_number.py b/tests/test_number.py index 5fb12fa6..5de454cf 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -332,6 +332,10 @@ def test_clamp(test_args: list[typing.Any], expected: str) -> None: ([1.235e29, "A"], "124 RA"), ([2.56e-30, "V"], "2.56 qV"), ([2.596e32, "F"], "260 QF"), + ([9.999e30, "W"], "10.0 QW"), + ([-9.999e30, "W"], "-10.0 QW"), + ([9.999e31, "W"], "100 QW"), + ([9.999e32, "W"], "1000 QW"), ([1e50], "1.00 x 10⁵⁰"), ([1e-50], "1.00 x 10⁻⁵⁰"), ([-1500, "V"], "-1.50 kV"),