Skip to content

Emit optional (#) fraction digits in DecimalFormat - #19

Open
selimfedakar wants to merge 1 commit into
rhasspy:mainfrom
selimfedakar:fix/decimal-format-optional-fraction-digits
Open

selimfedakar wants to merge 1 commit into
rhasspy:mainfrom
selimfedakar:fix/decimal-format-optional-fraction-digits

Conversation

@selimfedakar

Copy link
Copy Markdown
Contributor

Emit optional (#) fraction digits in DecimalFormat

What

format_decimal emitted the fractional part only when min_fraction_digits > 0, where min_fraction_digits is the count of required 0s in the pattern's fractional section. 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.#")   # -> "1"   (expected "1.5")

This is user-reachable end to end. #15 wired =<DecimalFormat pattern>= replace rules through format_decimal, which routes CLDR's x.x ordinal 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:

# en, de, and 49 other bundled languages whose x.x rule is "=#,##0.#=":
format_number(1.5, ruleset_names=["spellout-ordinal"])   # -> "1"   (expected "1.5")

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. A 0 is a required digit and is always shown. This fix completes the chain #15 started.

Why

The emit-the-dot decision was coupled to required-zero padding. min_fraction_digits counts only the required 0 places, so an all-# fraction produced a count of 0 and the whole fraction was discarded, even when the number genuinely had fractional digits.

Fix

Decouple "emit the decimal point" from required-zero padding:

  • still pad the fractional string out to the required 0 places, then
  • emit {integer}.{fraction} whenever the fractional value is actually non-empty, and a bare integer otherwise.

Integers still render with no trailing dot (1.0 with #,##0.# -> "1", never "1."). Required-zero patterns are unchanged (1.0 with #,##0.0 -> "1.0"). Grouping and integer padding are untouched. The change is about seven lines in format_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

  • Differential vs ICU (PyICU 68). Over the tested grid of values and patterns, optional-fraction patterns (.#, .##) now match ICU's output where they previously diverged by dropping the fraction; required-digit patterns (.0) are unchanged.
  • Regression test. Added 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").
  • Revert check. Reverting only unicode_rbnf/decimal_format.py while keeping the test makes exactly the new test fail (1.5 -> 1); nothing else regresses.
  • Suite. python -m pytest -q goes 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.5 spoken as 1). The fix restores optional-digit fraction emission to match ICU with no API change and every required-digit pattern byte-identical.

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

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