Skip to content
Open
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
3 changes: 2 additions & 1 deletion src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ def scientific(value: NumberOrString, precision: int = 2) -> str:
return _format_not_finite(value)
except (ValueError, TypeError):
return str(value)
fmt = f"{{:.{int(precision)}e}}"
# max(0): a negative precision builds an invalid format spec ("{:.-1e}").
fmt = f"{{:.{max(0, int(precision))}e}}"
n = fmt.format(value)
part1, part2 = n.split("e")
# Normalise exponent: int() strips the "+" sign and leading zeros,
Expand Down
9 changes: 9 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ def test_fractional(test_input: float | str, expected: str) -> None:
([-math.inf], "-Inf"),
(["nan"], "NaN"),
(["-inf"], "-Inf"),
# Negative precision clamps to zero instead of building an invalid spec.
([1e300, -1], "1 x 10³⁰⁰"),
([1e300, -5], "1 x 10³⁰⁰"),
([-1000, -1], "-1 x 10³"),
],
)
def test_scientific(test_args: list[typing.Any], expected: str) -> None:
Expand Down Expand Up @@ -310,6 +314,11 @@ def test_clamp(test_args: list[typing.Any], expected: str) -> None:
([math.nan, "m"], "NaN"),
([math.inf], "+Inf"),
([-math.inf], "-Inf"),
# precision=0 on a scientific-fallback magnitude passed -1 to scientific(); see #159.
([1e300, "m", 0], "1 x 10³⁰⁰m"),
([-1e300, "m", 0], "-1 x 10³⁰⁰m"),
([1e-300, "m", 0], "1 x 10⁻³⁰⁰m"),
([1e40, "", 0], "1 x 10⁴⁰"),
],
ids=str,
)
Expand Down