Skip to content

Fix intcomma() returning scientific notation for large floats - #449

Open
Vinayak19112003 wants to merge 1 commit into
python-humanize:mainfrom
Vinayak19112003:fix-intcomma-scientific-431
Open

Vinayak19112003 wants to merge 1 commit into
python-humanize:mainfrom
Vinayak19112003:fix-intcomma-scientific-431

Conversation

@Vinayak19112003

Copy link
Copy Markdown

Closes #431.

intcomma() used f"{value:,}" for floats, which formats via repr(). That switches to scientific notation at 1e16 (and for tiny floats like 1e-5), so no thousands separators were added:

>>> intcomma(1e16)
'1e+16'

The float is now expanded to fixed-point via Decimal(repr(value)) before grouping, so the full decimal representation gets commas:

>>> intcomma(1e16)
'10,000,000,000,000,000.0'
>>> intcomma(1.5e20)
'150,000,000,000,000,000,000.0'
>>> intcomma(0.00001)
'0.00001'

The ndigits path already used fixed-point formatting and is unchanged, as is the integer path.

Tests: new test_intcomma_large_float_no_scientific_notation (6 cases: 1e15/1e16/1.5e20/negative/tiny/ndigits=0) — fails on main, passes with the fix. Full tests/test_number.py: 247 passed. Ruff check and format clean.

@Wkhanex Wkhanex left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Additional value-preservation check: six large, small, signed and signed-zero floats (1e16, -1e16, 1.5e20, 1e-5, -1e-5, -0.0) all rendered without scientific notation. Parsing the output after removing grouping commas reproduced Decimal(repr(value)) exactly. This checks that expanding the shortest float representation adds grouping without introducing extra rounding, including negative tiny values. I ran these checks on Python 3.12 with English locale hooks; no full localization or pytest run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

intcomma returns scientific notation for large floats

2 participants