Formatting Numbers
For numbers, the message style can be divided in three parts separated by a semi-colon ::
-
The style type, either
integer,percent,currencyorexponential; -
The style primary option, only for:
-
the
currencytype, as:USDincurrency:USD. For more details on valid currencies, please refer to the official ICU documentation. -
the
exponentialtype, as:2inexponential:2, to limit its length. This option is not mandatory.
-
-
the style secondary option, which is the
attribute.scale/<multiplier>
If no option is appended, the semi-colon : following the type or primary option is not mandatory. However, if a secondary option is enabled, even without a primary option, both semi-colons are mandatory.
Here are some examples:
| Format Style | Message Example | Result Example (en-US) |
|---|---|---|
| integer |
{0,number,integer}
|
3.14159 → "3"
|
| percent |
{0,number,percent}
|
3.14159 → "314%"
|
| currency |
{0,number,currency:USD}
|
3.14159→ "$3.14"
|
| exponential |
{0,number,exponential}
|
3.14159 → "3.14159e+0"
|
Decimal format patterns are also supported, e.g. truncated decimal values. These patterns are composed of an integral part and an optional fractional part. The integral part is composed of a sequence of #, denoting optional digits, followed by a sequence of 0, denoting mandatory digits (they will be replaced by the character 0 if they are missing). The fractional part starts with the decimal separator character ., is followed by a sequence of 0 (mandatory digits), followed itself by a sequence of # (optional digits).
Here are some examples of valid decimal formats:
| Message Example | Result Example (en-US) |
|---|---|
{0, number, #.##}
|
3.14159 → "3.14"
|
{0,number, #.000}
|
3.14159 → "3.140"
|
{0,number, 00.#}
|
3.14159→ "03.1"
|
Here are some examples of valid exponential formats:
| Message Example | Result Example (en-US) |
|---|---|
{0,number, exponential}
|
314.159 → "3.14159e+2"
|
{0,number, exponential:0}
|
314.159 → "3e+2"
|
{0,number, exponential:3}
|
314.159 → "3.142e+2"
|
Finally, the secondary option scale/<multiplier> allows multiplying the base value upon formatting. For example, the message {0, number, 0.0015::scale/100}% would display as "0.15%".
Note that, as mentioned above, if the secondary option scale is enabled, even without a primary option, both semi-colons are mandatory.
Here are some examples of valid scale formats:
| Value | Message Example | Result Example (en-US) |
|---|---|---|
| 4 |
{0, number, exponential:2:scale/100}
|
4.00e+2
|
| 4 |
{0, number, percent::scale/0.01}
|
4%
|
| 4 |
{0, number, 0.00::scale/100}
|
400.00
|
| 4 |
{0, number, #.##::scale/100}
|
400
|
| 4321 |
{0, number, integer::scale/0.1}
|
432
|
| 40 |
{0, number, ::scale/1000}K
|
40,000K
|