Skip to content

intcomma: expand floats whose repr uses scientific notation - #484

Closed
mayur-dhavan wants to merge 2 commits into
python-humanize:mainfrom
mayur-dhavan:mayur-dhavan-patch-1
Closed

mayur-dhavan wants to merge 2 commits into
python-humanize:mainfrom
mayur-dhavan:mayur-dhavan-patch-1

Conversation

@mayur-dhavan

Copy link
Copy Markdown

Fixes #431.

Problem

intcomma formats floats with f"{value:,}", which uses repr-style general formatting and switches to an exponent at 1e16 (and below 1e-4), so no commas are added:

>>> humanize.intcomma(1e16)
'1e+16'            # before
'10,000,000,000,000,000.0'   # after
>>> humanize.intcomma(0.00001)
'1e-05'            # before
'0.00001'          # after

Fix

When the grouped result contains an exponent marker, reformat through Decimal(repr(value)), which never uses scientific notation. Integral floats keep their trailing .0, matching the existing output for values below 1e16 (e.g. intcomma(1e15) stays '1,000,000,000,000,000.0'). The ndigits path and string inputs are unchanged.

Tests

Seven new parametrized cases in test_intcomma (1e16, 1.5e20, -1e16, 1e16 with ndigits=0, 0.00001, 1.23e-5, 1234.5e15). tests/test_number.py passes locally (248 passed) and the full suite passes except the pre-existing test_benchmarks.py collection errors, which require the optional pytest-benchmark plugin and fail identically on unmodified master.

AI disclosure: this contribution was prepared with AI assistance (an automated coding agent acting on behalf of the author); the change and tests were validated locally against the project's test suite as described above.

Enhance number formatting to handle scientific notation.
Added test cases for floats in scientific notation to ensure correct formatting.
Copilot AI balanced review requested due to automatic review settings October 25, 2026 18:41

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@mayur-dhavan

Copy link
Copy Markdown
Author

Closing this as a duplicate of #432 (Oct 2), which fixes #431 with the same approach (formatting exponent-form floats through Decimal(str(value)) so they expand to grouped digits, preserving the trailing .0). I missed it when checking for existing work. Sorry for the noise.

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