Skip to content

feat(experimental/dimension): Add cash formatting option to CurrencyFormatter#8175

Open
younies wants to merge 26 commits into
unicode-org:mainfrom
younies:currency-cash-formatting
Open

feat(experimental/dimension): Add cash formatting option to CurrencyFormatter#8175
younies wants to merge 26 commits into
unicode-org:mainfrom
younies:currency-cash-formatting

Conversation

@younies

@younies younies commented Jul 3, 2026

Copy link
Copy Markdown
Member

This PR implements the cash formatting option for CurrencyFormatter and CompactCurrencyFormatter.

Diff Base: #8169.

Changelog

  1. Add CurrencyUsage Enum:

    • Defined CurrencyUsage in options.rs with Standard and Cash variants.
    • Replaced the temporary cash: bool option in CurrencyFormatterOptions with usage: CurrencyUsage.
  2. Implement Cash Rounding Fallback:

    • Updated resolve_fraction_info in both CurrencyFormatter and CompactCurrencyFormatter to check if options.usage is CurrencyUsage::Cash.
    • If cash usage is specified, it overrides the standard precision (digits and rounding) with cash_digits and cash_rounding from the resolved FractionInfo (if they are present in the data).
  3. Update Constructors:

    • Modified CurrencyFormatter constructors (try_new_short, try_new_narrow, try_new_long and their unstable variants) to accept CurrencyFormatterOptions so that callers can configure the usage.
  4. Tests:

    • Updated existing tests and doc-tests to pass Default::default() (which uses Standard usage).
    • Added a new test case test_cash_rounding in format.rs using CHF (Swiss Franc) to verify that CurrencyUsage::Cash correctly rounds values to the nearest 0.05 (e.g., 1.23 -> 1.25, 1.22 -> 1.20) while Standard usage keeps 2 decimal places (1.23 and 1.22).

TAG=agy
CONV=f08dbbb0-24e0-4da6-a2f8-f6a19ba2c8b5

younies added 25 commits July 2, 2026 11:11
Implement the ValueRepresentation trait and Decimal marker struct.
Migrate CurrencyFormatter to CurrencyFormatter<Decimal> with
try_new_short and try_new_narrow constructors, removing the old
non-generic try_new constructors. Update tests accordingly.

TAG=agy
CONV=0f8e0e91-077a-4704-b63f-0c02f37cbc41
- Update try_new_narrow and try_new_narrow_unstable to use robust fallback loading, ensuring consistency with short constructors and supporting numbering system overrides.
- Add explanatory comments and TODOs for the manual constructors explaining the cross-crate dependency constraint.

TAG=agy
CONV=0f8e0e91-077a-4704-b63f-0c02f37cbc41
…sentation

- Add TODO(unicode-org#8146) to format_fixed_decimal to track the discussion about whether FixedDecimal is the correct input type for currency formatting.

TAG=agy
CONV=0f8e0e91-077a-4704-b63f-0c02f37cbc41
- Remove test_en_us_aud to keep the test suite DRY, addressing reviewer feedback. CAD and USD tests already provide full coverage for the dollar currency prefix and narrow/short resolution logic.

TAG=agy
CONV=0f8e0e91-077a-4704-b63f-0c02f37cbc41
- Rename locale variable to prefs of type CurrencyFormatterPreferences across all test cases in format.rs for naming consistency and to address reviewer feedback.

TAG=agy
CONV=0f8e0e91-077a-4704-b63f-0c02f37cbc41
- Add test cases to test_numbering_system_override to verify that try_new_narrow correctly respects numbering system overrides (like ar-EG-u-nu-latn), ensuring consistency with try_new_short. - Rename short test variables for clarity.

TAG=agy
CONV=0f8e0e91-077a-4704-b63f-0c02f37cbc41
Revert manual precision additions to CurrencyEssentials and
CurrencyExtendedData, and instead load and use the existing
CurrencyFractionsV1 singleton to resolve currency precision.

Also apply a fix to check_pattern in essentials provider to allow
mismatches in grouping separators (Indian vs Western).

TAG=agy
CONV=4e806c61-d22c-4821-8a7f-6c8e0ebb5a29
Introduce CurrencyPatternFractionsV1 marker to store precision extracted
from CLDR currency patterns at datagen time.

Update CurrencyFormatter and CompactCurrencyFormatter to load this data
and implement a 3-step fallback for resolving currency precision:
1. Global currency-specific overrides (from CurrencyFractionsV1).
2. Locale-specific pattern precision (from CurrencyPatternFractionsV1).
3. Global default (2 decimals).

TAG=agy
CONV=4e806c61-d22c-4821-8a7f-6c8e0ebb5a29
Update CurrencyFormatter to make CurrencyExtendedDataV1 (display names)
optional. If the extended data for a currency is missing (e.g., for a
fake or unsupported currency), we now fall back to using its ISO code
as the display name, instead of failing construction.

Add test_long_fallback_to_iso to verify this behavior.

TAG=agy
CONV=4e806c61-d22c-4821-8a7f-6c8e0ebb5a29
Following the decision that Long formatting does not need locale-specific
pattern precision fallback (as it has no pattern-defined precision and
should fall back to the global default), we merge the pattern precision
data (fractions_vec, standard_index, standard_next_to_alpha) directly
into CurrencyEssentials.

This optimizes Short and Narrow formatting by reducing the number of
loaded data markers from 2 to 1, while Long formatting remains optimized
by only loading CurrencyFractionsV1 (global overrides).

Updates:
- Removed CurrencyPatternFractionsV1 marker.
- Added fractions_vec, standard_index, standard_next_to_alpha to CurrencyEssentials.
- Updated datagen to populate these fields in CurrencyEssentialsV1.
- Updated CurrencyFormatter and CompactCurrencyFormatter to use CurrencyEssentials for locale-specific precision.
- Regenerated baked data.

TAG=agy
CONV=4e806c61-d22c-4821-8a7f-6c8e0ebb5a29
- Pass CurrencyCode by value in CompactCurrencyFormatter::resolve_fraction_info.
- Collapse nested if statements using Option::filter in resolve_fraction_info (both formatters).
- Remove unused load_with_fallback from compact_formatter.rs.
- Remove unused ZeroVec import from fractions.rs.
- Remove unnecessary qualifications in essentials.rs (provider).
- Remove needless borrow of locale in essentials.rs (provider).

TAG=agy
CONV=4e806c61-d22c-4821-8a7f-6c8e0ebb5a29
This aligns the function name with its return type (FractionInfo) and makes
it more descriptive of what it actually does.

TAG=agy
CONV=4e806c61-d22c-4821-8a7f-6c8e0ebb5a29
Added comments in formatter.rs referencing UTS unicode-org#35 Section 3.11.1
explaining that we fall back to the currency code itself if the
displayName is not found.

TAG=agy
CONV=4e806c61-d22c-4821-8a7f-6c8e0ebb5a29
TAG=agy
CONV=4e806c61-d22c-4821-8a7f-6c8e0ebb5a29
TAG=agy
CONV=4e806c61-d22c-4821-8a7f-6c8e0ebb5a29
@younies younies changed the title Currency cash formatting feat(experimental/dimension): Add cash formatting option to CurrencyFormatter Jul 3, 2026
@younies younies marked this pull request as ready for review July 3, 2026 14:33
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