Number Formatting in ICU Messages
Format numbers with locale-aware thousands separators, currency symbols, percentages, and integer rounding using ICU number arguments. Covers all built-in number styles.
Detailed Explanation
Locale-Aware Number Formatting
The number argument type in ICU MessageFormat formats numeric values according to the rules of the target locale. This handles thousands separators, decimal points, currency symbols, and percentage formatting automatically.
Basic Number Formatting
{count, number}
| Locale | Input | Output |
|---|---|---|
| en-US | 1234567.89 | 1,234,567.89 |
| de-DE | 1234567.89 | 1.234.567,89 |
| ja-JP | 1234567.89 | 1,234,567.89 |
| fr-FR | 1234567.89 | 1 234 567,89 |
Number Styles
ICU defines several built-in number styles:
Currency
The total is {amount, number, currency}.
| Locale | Input | Output |
|---|---|---|
| en-US | 42.5 | $42.50 |
| de-DE | 42.5 | 42,50 EUR |
| ja-JP | 42.5 | 43 JPY |
Currency formatting adapts symbol placement, decimal digits, and the currency code based on the locale.
Percent
Completion: {ratio, number, percent}
| Locale | Input | Output |
|---|---|---|
| en-US | 0.856 | 86% |
| fr-FR | 0.856 | 86 % |
Note: The input value is treated as a fraction (0.856 = 85.6%), not as a percentage already.
Integer
About {count, number, integer} items
Rounds to the nearest integer:
- 42.7 -> 43
- 42.2 -> 42
In Sentences
Combine number formatting with surrounding text:
Your cart: {itemCount, number} {itemCount, plural, one {item} other {items}} totaling {total, number, currency}.
With itemCount=3 and total=149.99 in en-US: "Your cart: 3 items totaling $149.99."
Why Not Format in Code?
Formatting numbers in application code before passing them to the message breaks the localization contract. The translator has no control over how numbers appear in different locales. Always pass raw numeric values and let the ICU formatter handle locale-specific formatting.
Custom Number Patterns
Some ICU implementations support custom number patterns using Unicode number format syntax:
{amount, number, ::currency/EUR}
{ratio, number, ::.00}
However, support for custom patterns varies by library. The standard styles (currency, percent, integer) are universally supported.
Use Case
E-commerce developers building multi-currency storefronts, dashboard developers displaying metrics, or any application showing locale-formatted numbers like prices, percentages, and counts.