Skip to content

format_compact_decimal ignores fraction_digits below the smallest compact magnitude #1484

Backlog

Description

@chrikrah

Overview Description

format_compact_decimal and format_compact_currency ignore fraction_digits below the smallest compact magnitude and on a "0" pattern. Both docstrings say it "Defaults to 0" (babel/numbers.py:636, :922).

Steps to Reproduce

  1. python scripts/download_import_cldr.py on master at aeca9fa.
  2. Run:
from babel.numbers import format_compact_decimal, format_compact_currency

print(format_compact_decimal(12.345, locale="en_US"))
print(format_compact_decimal(999.6, locale="en_US"))
print(format_compact_currency(0.123, "USD", locale="en_US"))
print(format_compact_decimal(1234.5, locale="de_DE"))

Actual Results

12.345
999.6
$0.123
1234,5

Expected Results

12
1K
$0
1234

ICU agrees, rounding half-even like babel:

$ node -e 'const f=(l,o,v)=>new Intl.NumberFormat(l,{notation:"compact",maximumFractionDigits:0,roundingMode:"halfEven",...o}).format(v);console.log(process.versions.icu,f("en-US",{},12.345),f("en-US",{},999.6),f("en-US",{style:"currency",currency:"USD"},0.123),f("de-DE",{},1234.5))'
78.3 12 1K $0 1234

Reproducibility

Always, Python 3.15.2, Linux.

Additional Information

Neither the fallback (babel/numbers.py:644-652) nor the "0" branch (line 680) rounds, though #909 set decimal_quantization=False there assuming the number is always rounded (#909 (comment)). Rounding prints $0.123 as $0 by default, as at 1K; fraction_digits=2 keeps cents. Should I open a pull request that rounds in both paths?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions