Skip to content

Commit 3690ec5

Browse files
committed
Fix significant figures when metric rounding carries within quetta
1 parent 392aef7 commit 3690ec5

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

‎src/humanize/number.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ def metric(value: float, unit: str = "", precision: int = 3) -> str:
582582
value /= 10**old_bucket
583583
digits = int(max(0, precision - exponent % 3 - 1))
584584
# Absorb a rounding carry (9.999 -> 10.0) by bumping the exponent.
585-
if exponent < 30 and round(abs(value), digits) >= 10 ** (exponent % 3 + 1):
585+
if exponent < 32 and round(abs(value), digits) >= 10 ** (exponent % 3 + 1):
586586
exponent += 1
587587
new_bucket = exponent // 3 * 3
588588
value /= 10 ** (new_bucket - old_bucket)

‎tests/test_number.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ def test_clamp(test_args: list[typing.Any], expected: str) -> None:
332332
([1.235e29, "A"], "124 RA"),
333333
([2.56e-30, "V"], "2.56 qV"),
334334
([2.596e32, "F"], "260 QF"),
335+
([9.999e30, "W"], "10.0 QW"),
336+
([-9.999e30, "W"], "-10.0 QW"),
337+
([9.999e31, "W"], "100 QW"),
338+
([9.999e32, "W"], "1000 QW"),
335339
([1e50], "1.00 x 10⁵⁰"),
336340
([1e-50], "1.00 x 10⁻⁵⁰"),
337341
([-1500, "V"], "-1.50 kV"),

0 commit comments

Comments
 (0)