Repository navigation
Conversation
Fixes python-humanize#496 intcomma() converted decimal strings with float(value), losing precision on large values (e.g. '1234567890123456.78' -> '...456.8'). Keep the exact string for the ndigits=None path and use Decimal when ndigits is given, preserving all input digits. Co-authored-by: CommandCodeBot <noreply@commandcode.ai>
Ekko82
left a comment
There was a problem hiding this comment.
Compared the public humanize.intcomma API on base d16e6352 and head 25087f91 with CPython 3.13.9 on Windows, after reading the complete two-file diff. The large-decimal cases pass on head; the inline note covers two exponent-string regressions.
Validation covered eight behavior cases per ref with normal package imports. The full suite and translated locales were not run. Current Actions runs report action_required.
| result = f"{value:,}" | ||
| if is_str_decimal: | ||
| # Format the original digit string directly so no precision is lost. | ||
| int_part, _, frac_part = value.partition(".") |
There was a problem hiding this comment.
[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.
Fixes #496
intcomma('1234567890123456.78')returned'1,234,567,890,123,456.8'— the last digit of the fraction was lost because the string branch converted viafloat(), which can't represent 16-digit integers with paise precisely. ADecimalinput already worked correctly.Fix: for decimal strings, keep the original digit string for the
ndigits=Nonepath (group the integer part, keep the fraction verbatim) and convert throughDecimalwhenndigitsis specified so explicit rounding still works without float rounding.Added parametrized cases in
tests/test_number.pyfor both branches.Verified locally:
tests/test_number.py— 243 passed.Co-authored-by: CommandCodeBot noreply@commandcode.ai