Repository navigation
Emit optional (#) fraction digits in DecimalFormat - #19
Open
selimfedakar wants to merge 1 commit into
Open
selimfedakar wants to merge 1 commit into
selimfedakar wants to merge 1 commit into
Conversation
format_decimal only emitted the fractional part when min_fraction_digits (the count of required "0"s in the pattern's fraction) was greater than zero. For an all-optional fraction pattern like "#,##0.#" that count is 0, so a real non-zero fraction was dropped: format_decimal(1.5, "#,##0.#") returned "1". Since rhasspy#15 routes CLDR "=#,##0.#=" replace rules into format_decimal, this dropped the fraction when spelling out the ordinal of any non-integer in the 51 bundled languages whose x.x rule is "=#,##0.#=". Decouple emitting the decimal point from required-zero padding: still pad required "0" places, but emit "{int}.{frac}" whenever the value actually has fractional digits, and a bare integer otherwise. ICU treats "#" as an optional digit (shown when non-zero, omitted when zero); integers still render with no trailing dot, and required-zero patterns (".0") are unchanged. Scope: this fixes optional-digit (#) fraction emission only. It does not change the module's rounding mode (Python's default {:.Nf}), which is a separate, pre-existing matter and is not claimed here.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Emit optional (#) fraction digits in DecimalFormat
What
format_decimalemitted the fractional part only whenmin_fraction_digits > 0, wheremin_fraction_digitsis the count of required0s in the pattern's fractional section. For an all-optional fraction pattern like#,##0.#that count is0, so a real, non-zero fraction was dropped:This is user-reachable end to end.
#15wired=<DecimalFormat pattern>=replace rules throughformat_decimal, which routes CLDR'sx.xordinal rule — keyed=#,##0.#=in 51 of the bundled languages — into this code path. So spelling out the ordinal of any non-integer dropped the fraction in those languages:ICU treats a
#after the decimal point as an optional digit: shown when the rounded value has a non-zero digit in that place, omitted when it does not. A0is a required digit and is always shown. This fix completes the chain#15started.Why
The emit-the-dot decision was coupled to required-zero padding.
min_fraction_digitscounts only the required0places, so an all-#fraction produced a count of0and the whole fraction was discarded, even when the number genuinely had fractional digits.Fix
Decouple "emit the decimal point" from required-zero padding:
0places, then{integer}.{fraction}whenever the fractional value is actually non-empty, and a bare integer otherwise.Integers still render with no trailing dot (
1.0with#,##0.#->"1", never"1."). Required-zero patterns are unchanged (1.0with#,##0.0->"1.0"). Grouping and integer padding are untouched. The change is about seven lines informat_decimal.Scope (explicit)
This fixes optional-digit (
#) fraction emission only. It does not change the module's rounding mode. The module rounds with Python's{:.Nf}(round-half-to-even on binary floats); that is a separate, pre-existing matter and no rounding-parity claim is made here.Verification
.#,.##) now match ICU's output where they previously diverged by dropping the fraction; required-digit patterns (.0) are unchanged.test_format_decimal_optional_fraction_digits, covering#,##0.#(1.5->"1.5",0.5->"0.5",2.56->"2.6",1.0->"1",-1.5->"-1.5"), two optional places#,##0.##(2.56->"2.56"), and the required-digit control#,##0.0(1.0->"1.0").unicode_rbnf/decimal_format.pywhile keeping the test makes exactly the new test fail (1.5->1); nothing else regresses.python -m pytest -qgoes from 135 to 136 passing, 0 regressions.Impact
This library backs Rhasspy / Home Assistant TTS, so the dropped fraction was read aloud: spelling out the ordinal of a non-integer in any of the 51 affected languages lost the fractional part entirely (
1.5spoken as1). The fix restores optional-digit fraction emission to match ICU with no API change and every required-digit pattern byte-identical.