Skip to content

Raise a clear ValueError for negative ndigits in intcomma - #468

Closed
ozenyunus35 wants to merge 1 commit into
python-humanize:mainfrom
ozenyunus35:fix/intcomma-negative-ndigits
Closed

ozenyunus35 wants to merge 1 commit into
python-humanize:mainfrom
ozenyunus35:fix/intcomma-negative-ndigits

Conversation

@ozenyunus35

Copy link
Copy Markdown

Fixes #466

Changes proposed in this pull request:

  • intcomma(value, ndigits) with a negative ndigits used to fail with ValueError: Format specifier missing precision from the f-string. It now raises ValueError: ndigits must be >= 0, got -1 before doing anything else, as suggested in the issue, and the docstring lists it under Raises.
  • Added test_intcomma_negative_ndigits for float, int, numeric string and None values; it fails on main.

An alternative would be to round to tens/hundreds like round(x, -1), but the docstring describes ndigits as digits after the decimal point, so an error seemed like the smaller change. Happy to switch if you'd prefer that.

I used an AI assistant to help draft parts of the code and tests; I reviewed the changes and ran the tests myself.

@hugovk

hugovk commented Oct 22, 2026

Copy link
Copy Markdown
Member

Duplicate of #467, as you can see linked from #466.

@hugovk hugovk closed this Oct 22, 2026
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(num, ndigits=-1) ValueError: Format specifier missing precision

2 participants