Skip to content
Closed
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
20 changes: 18 additions & 2 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,20 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
str: String containing commas every three digits.
"""
import math
from decimal import Decimal

thousands_sep = thousands_separator()
decimal_sep = decimal_separator()
is_str_decimal = False
try:
if isinstance(value, str):
value = value.replace(thousands_sep, "").replace(decimal_sep, ".")
if not math.isfinite(float(value)):
return _format_not_finite(float(value))
if "." in value:
value = float(value)
# Keep the exact decimal string to avoid float() rounding
# large values, e.g. '1234567890123456.78'.
is_str_decimal = True
else:
value = int(value)
elif not isinstance(value, int):
Expand All @@ -195,9 +199,21 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
return str(value)

if ndigits is not None:
if is_str_decimal:
value = Decimal(value)
result = f"{value:,.{ndigits}f}"
else:
result = f"{value:,}"
if is_str_decimal:
# Format the original digit string directly so no precision is lost.
int_part, _, frac_part = value.partition(".")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Keep formatting exponent strings with a decimal point

frac_part also contains any exponent suffix. With ndigits=None, intcomma('1.2e3') now returns '1.2e3' instead of the base result '1,200.0'; intcomma('1.2e-3') likewise changes from '0.0012' to '1.2e-3'. These inputs still format correctly when ndigits is explicit because that path uses Decimal. Please preserve exponent normalization in this branch and include these two cases alongside the new precision tests.

sign = ""
if int_part.startswith(("-", "+")):
sign, int_part = int_part[0], int_part[1:]
int_part = int_part.lstrip("0") or "0"
grouped = f"{int(int_part):,}" if int_part else "0"
result = f"{sign}{grouped}.{frac_part}" if frac_part else f"{sign}{grouped}"
else:
result = f"{value:,}"
if thousands_sep != "," or decimal_sep != ".":
result = result.translate(str.maketrans(",.", thousands_sep + decimal_sep))
return result
Expand Down
2 changes: 2 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def test_ordinal(test_input: str, expected: str) -> None:
(["10311"], "10,311"),
(["1000000"], "1,000,000"),
(["1234567.1234567"], "1,234,567.1234567"),
(["1234567890123456.78"], "1,234,567,890,123,456.78"),
(["1234567890123456.78", 2], "1,234,567,890,123,456.78"),
(["1234567.1234567", 0], "1,234,567"),
(["1234567.1234567", 1], "1,234,567.1"),
(["1234567.1234567", 10], "1,234,567.1234567000"),
Expand Down