From 43145be484c829e1142d1856520e3e9980d1dd17 Mon Sep 17 00:00:00 2001 From: itzzdev09 Date: Fri, 2 Oct 2026 12:20:16 +0530 Subject: [PATCH 1/2] Format integers beyond float range in scientific() --- src/humanize/number.py | 16 +++++++++++----- tests/test_number.py | 5 +++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 52a5356a..f371d7a4 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -443,14 +443,20 @@ def scientific(value: NumberOrString, precision: int = 2) -> str: """ import math + fmt = f"{{:.{int(precision)}e}}" try: - value = float(value) - if not math.isfinite(value): - return _format_not_finite(value) + number = float(value) + except OverflowError: + # An integer beyond float's range: Decimal has no such limit. + from decimal import Decimal + + n = fmt.format(Decimal(value)) except (ValueError, TypeError): return str(value) - fmt = f"{{:.{int(precision)}e}}" - n = fmt.format(value) + else: + if not math.isfinite(number): + return _format_not_finite(number) + n = fmt.format(number) part1, part2 = n.split("e") # Normalise exponent: int() strips the "+" sign and leading zeros, # while preserving "-" and a single "0" for 10⁰. diff --git a/tests/test_number.py b/tests/test_number.py index 5fb12fa6..f424f0a5 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -271,6 +271,11 @@ def test_fractional(test_input: float | str, expected: str) -> None: ([-math.inf], "-Inf"), (["nan"], "NaN"), (["-inf"], "-Inf"), + ([10**400], "1.00 x 10⁴⁰⁰"), + ([-(10**400)], "-1.00 x 10⁴⁰⁰"), + ([12345 * 10**400, 3], "1.234 x 10⁴⁰⁴"), + ([10**400, 0], "1 x 10⁴⁰⁰"), + ([0], "0.00 x 10⁰"), ], ) def test_scientific(test_args: list[typing.Any], expected: str) -> None: From 40a2faf0c6981cbdfd4d1cebe64ee1e7c4ec9725 Mon Sep 17 00:00:00 2001 From: itzzdev09 Date: Fri, 20 Nov 2026 21:05:51 +0530 Subject: [PATCH 2/2] Format numeric strings beyond float range in scientific() float() turns a numeric string that is too large into infinity and one that is too small into zero, so scientific() rendered '1' followed by 400 zeros as +Inf and '1e-400' as 0. Format such strings with Decimal. --- src/humanize/number.py | 13 +++++++++---- tests/test_number.py | 4 ++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index f371d7a4..e9b672fd 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -442,21 +442,26 @@ def scientific(value: NumberOrString, precision: int = 2) -> str: str: Number in scientific notation z.wq x 10ⁿ. """ import math + from decimal import Decimal fmt = f"{{:.{int(precision)}e}}" try: number = float(value) except OverflowError: # An integer beyond float's range: Decimal has no such limit. - from decimal import Decimal - n = fmt.format(Decimal(value)) except (ValueError, TypeError): return str(value) else: - if not math.isfinite(number): + out_of_range = number == 0 or not math.isfinite(number) + exact = Decimal(value) if isinstance(value, str) and out_of_range else None + if exact is not None and exact.is_finite() and exact != 0: + # float() turns a string beyond its range into infinity or zero. + n = fmt.format(exact) + elif not math.isfinite(number): return _format_not_finite(number) - n = fmt.format(number) + else: + n = fmt.format(number) part1, part2 = n.split("e") # Normalise exponent: int() strips the "+" sign and leading zeros, # while preserving "-" and a single "0" for 10⁰. diff --git a/tests/test_number.py b/tests/test_number.py index f424f0a5..a67327ac 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -275,6 +275,10 @@ def test_fractional(test_input: float | str, expected: str) -> None: ([-(10**400)], "-1.00 x 10⁴⁰⁰"), ([12345 * 10**400, 3], "1.234 x 10⁴⁰⁴"), ([10**400, 0], "1 x 10⁴⁰⁰"), + (["1" + "0" * 400], "1.00 x 10⁴⁰⁰"), + (["-1" + "0" * 400], "-1.00 x 10⁴⁰⁰"), + (["1e400"], "1.00 x 10⁴⁰⁰"), + (["1e-400"], "1.00 x 10⁻⁴⁰⁰"), ([0], "0.00 x 10⁰"), ], )