Skip to content

Implement Currency Formatter API (New Design)#8045

Draft
younies wants to merge 4 commits into
unicode-org:mainfrom
younies:currency-complete
Draft

Implement Currency Formatter API (New Design)#8045
younies wants to merge 4 commits into
unicode-org:mainfrom
younies:currency-complete

Conversation

@younies

@younies younies commented Jun 8, 2026

Copy link
Copy Markdown
Member

Changelog

This PR implements the self-contained generic CurrencyFormatter API (Option 1) as part of the experimental currency formatting architecture.

Core Architecture & Typestate

  • ValueRepresentation Trait: Introduces a core trait to govern representation-specific internal data states.
  • Marker Structs: Implements Decimal, Compact, and Scientific (placeholder) marker types to enforce compile-time typestates.
  • Unified Formatter: Defines CurrencyFormatter<T: ValueRepresentation> which transparently adapts its internal storage based on the active typestate.

Data Models & Payloads

  • DecimalCurrencyData: Added internal enum to handle both Standard (CurrencyEssentialsV1) and Long (CurrencyExtendedDataV1 + CurrencyPatternsDataV1) data requirements.
  • CompactCurrencyData: Added internal enum integrating currency data with icu_decimal compact payloads (DecimalCompactShortV1 and DecimalCompactLongV1) and plural rules.

Preferences & Configuration

  • New Preference Types: Defines CurrencyFormatterPreferences and CompactCurrencyFormatterPreferences supporting custom numbering systems.
  • Macro Integrations: Implements prefs_convert! rules for zero-friction conversion into underlying DecimalFormatterPreferences, CompactDecimalFormatterPreferences, and PluralRulesPreferences.

Polymorphic Constructors

  • Standard Decimal: Added try_new_short, try_new_narrow, and try_new_long implementations on CurrencyFormatter<Decimal>.
  • Compact Formats: Added try_new_short, try_new_narrow, and try_new_long implementations on CurrencyFormatter<Compact>.
  • Marker Attributes: Handles dynamic derivation of DataMarkerAttributes from CurrencyCode to properly resolve locale-specific long display names.

Formatting Execution

  • format_fixed_decimal: Implements robust formatting logic returning impl Writeable + Display.
  • Pattern Interpolation: Handles multi-placeholder pattern resolution, seamlessly combining currency symbols/names with formatted numbers and compact significands.
  • Sign & Plurality: Correctly preserves value signs (FixedDecimal::sign) and evaluates PluralRules across all complex compact and long variants.

Testing & Verification

  • Added a full suite of unit tests validating all 6 primary configurations (Decimal/Compact x Short/Narrow/Long).
  • Verified expected textual outputs including exact symbol placement, rounding, compact suffixing, and pluralized long names (e.g., $12,345.67, 12,345.67 US dollars, $12K, 12 thousand US dollars).

@younies younies changed the title Implement Currency Formatter API New Design Implement Currency Formatter API (New Design) Jun 8, 2026
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