Skip to content

feat(currency): Add accounting formatting support to CurrencyFormatter<Compact>#8187

Open
younies wants to merge 2 commits into
unicode-org:mainfrom
younies:accounting-compact-currency
Open

feat(currency): Add accounting formatting support to CurrencyFormatter<Compact>#8187
younies wants to merge 2 commits into
unicode-org:mainfrom
younies:accounting-compact-currency

Conversation

@younies

@younies younies commented Jul 7, 2026

Copy link
Copy Markdown
Member

Summary

This PR implements accounting currency formatting support (e.g., formatting negative monetary values in parentheses like ($12K) in en-US) for compact currency formatting (CurrencyFormatter<Compact>).

This PR is stacked on top of #8182 (compact-currency-formatting).

Changelog

  1. Restore CurrencyFormatterOptions & CurrencyUsage:

  2. Accounting Pattern Resolution in CurrencyFormatter<Compact>:

    • Updates try_new_short / try_new_narrow (and unstable/buffer counterparts) on CurrencyFormatter<Compact> to accept options: CurrencyFormatterOptions.
    • In format_fixed_decimal, checks self.options.usage == CurrencyUsage::Accounting. When formatting a negative number, queries accounting_negative_pattern() (or accounting_alpha_next_to_number_negative_pattern()) from CurrencyEssentialsV1, falling back cleanly to standard negative patterns if no accounting pattern is defined for the locale.
    • Sign Wrapping Prevention: When an accounting negative pattern is applied (which already formats the sign via parentheses or symbols), passes Sign::None to DecimalFormatter::format_sign so it does not prepend a redundant minus sign (preventing -($12K)).
  3. Zero Conflicts with Standard Decimal Formatting (feat(currency): Add accounting formatting and relax constructor validation #8177):

  4. Testing:

    • Updates existing short/narrow compact test callers in compact_format.rs to pass Default::default().
    • Adds a new test_accounting unit test in compact_format.rs verifying that CurrencyUsage::Accounting formats -12345.67 as ($12K) in en-US.

Verification

  • cargo clippy -p icu_experimental --no-deps passed cleanly.
  • cargo fmt --all -- --check passed cleanly.
  • cargo test -p icu_experimental passed (all 49 unit tests and 40 doctests).

Fixes #8185

younies added 2 commits July 7, 2026 14:42
… and simplify options

This commit modernizes compact currency formatting in ICU4X by:
1. Architecture Migration: Migrating CompactCurrencyFormatter to adopt the generic CurrencyFormatter<Compact> design with try_new_short and try_new_narrow constructors (where currency is bound at construction time).
2. Options Simplification: Removing CurrencyFormatterOptions from options.rs and replacing it with a direct pub(crate) width: Width field across CurrencyFormatter<V>, as width is now determined at construction time.
3. Pattern Gluing: In format_fixed_decimal, we use name_and_pattern from CurrencyEssentialsV1 (which resolves standard vs standard_alpha_next_to_number patterns), format the number via compact decimal (compact_data), and glue the compact number with the currency symbol.

TAG=agy
CONV=c033380d-144b-4f6b-b091-b5c89c4f80a1
…r<Compact>

This commit implements accounting currency formatting (e.g. using parentheses for negative values in locales like en-US) for compact currency formatting (`CurrencyFormatter<Compact>`).

- Restores `CurrencyFormatterOptions` and `CurrencyUsage` (`Standard`, `Accounting`) in `options.rs`.
- Adds `options: CurrencyFormatterOptions` to `CurrencyFormatter<Compact>` constructors (`try_new_short` / `try_new_narrow`).
- Resolves accounting positive and negative patterns from `CurrencyEssentials` in `format_fixed_decimal`.
- Passes `Sign::None` to `DecimalFormatter::format_sign` when an accounting negative pattern has already formatted the negative sign.
- Keeps standard `Decimal` currency formatting untouched to ensure a clean merge with upstream changes.
- Adds comprehensive unit tests for accounting compact currency formatting.

Fixes unicode-org#8185

TAG=agy
CONV=c033380d-144b-4f6b-b091-b5c89c4f80a1
@younies younies force-pushed the accounting-compact-currency branch from 09475ef to c5c216c Compare July 7, 2026 14:54
@younies younies marked this pull request as ready for review July 7, 2026 15:14
@younies younies requested a review from robertbastian July 7, 2026 15:14

@robertbastian robertbastian left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs rebase

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.

currency: Add accounting option support for compact currency formatting (ECMA-402 compliance)

2 participants