Skip to content

Normalize scientific mantissas after rounding carries - #1363

Open
kokotatan wants to merge 1 commit into
python-babel:masterfrom
kokotatan:fix-scientific-rounding-carry
Open

kokotatan wants to merge 1 commit into
python-babel:masterfrom
kokotatan:fix-scientific-rounding-carry

Conversation

@kokotatan

Copy link
Copy Markdown

Scientific formatting normalizes the mantissa before rounding, so rounding across a power of ten can add an extra leading digit without updating the exponent. For example, format_scientific(Decimal("9.99"), "0.0E0", locale="en_US") currently produces 10.0E0; it should produce 1.0E1 with the pattern's two significant digits.

Normalize a finite mantissa that gains a leading digit during rounding, adjusting the exponent and its localized sign. The regressions cover positive/negative exponents, crossing zero with explicit exponent signs, exponent padding, multiple mandatory leading digits, negative numbers, Arabic numbering symbols, and preservation of non-carry, zero, infinity, and disabled quantization behavior.

The expected formatting follows the LDML scientific notation rules for integer digits and maximum significant digits. Related open PR #1281 changes initial exponent grouping; this fix addresses rounding carry with Babel's current normalization. PR #1286 addresses directional rounding of negative values, which is a separate issue.

Validation:

  • Regression tests before the fix: 9 failed, 4 passed.
  • Full suite on Python 3.13 after importing CLDR: 7844 passed, 9 skipped, 2 xfailed.
  • All 146 number formatting tests passed.
  • All pinned pre-commit hooks passed (all files before the final two context cases; changed files rechecked afterward).
  • Independently checked all eight Decimal rounding modes and near both Decimal exponent limits (66 checks).

This contribution was prepared with OpenAI Codex assistance and independently reviewed before submission.

This branch has not been deployed

No deployments
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.

1 participant