Number formatting
Index
Introduction
Each project has a default number format pattern and a default locale. Together, these determine how numbers are displayed, by default, when you generate a narrative.
The number format pattern determines how many decimal places are displayed, whether trailing zeros are shown, and whether thousand separators are used. The locale defines which characters are used to represent the decimal point and thousand separators in the output.
The default number format pattern and locale are defined in the Settings view (Settings > Number and Currency). The default pattern is #,##0.##
, which ensures numbers are displayed to two decimal places with trailing zeros stripped. The default locale is en_US, which uses a period (full stop) for the decimal point and a comma for thousand separators. The syntax for number format patterns is described below.
To change the default formatting for numbers, you can change the defaults in the Settings view. Alternatively, you can override the default settings — that is, apply different formatting to specific numbers — using formatting functions. This is also described below.
Important
Before Studio 3.1.0, the default behavior was to display numbers to seven decimal places with trailing zeros stripped. To ensure backward compatibility, all projects published before the release of 3.1.0 have their default number format pattern set to #,##0.#######
. These projects are also set to run in legacy mode. See Legacy behaviors.
Syntax for number format patterns
Number format patterns are defined using Java’s DecimalFormat syntax. The pattern is a sequence of special characters.
Special characters
The special characters for number formatting are:
Special Character | Description |
---|---|
| Prints a digit in this position, even when it's a trailing or leading zero. |
| Prints a digit in this position, unless it's a trailing or zero, in which case it prints nothing. |
| Defines the position and formatting of the currency unit. |
| Minus sign. |
| Defines the position of the decimal separator. |
| Defines the position of thousand separators. |
| Separates positive and negative subpatterns. |
The special characters used for separators in the pattern do not indicate which characters are displayed as separators in the output text; they merely define where the separators are in the pattern. The characters used for separators in the output text are determined by the locale.
The default locale is en_US (English, United States). This locale applies the convention of using a period (full stop) for the decimal separator and a comma for thousand separators. To apply a different convention, change the default locale in Settings > Number and Currency.
This table shows the affect of changing the locale or pattern:
Unformatted Number | Default Locale | Default Number Format Pattern | Formatted Number |
---|---|---|---|
1234.5678 | en_US |
| 1,234.6 |
1234.5678 | de_DE |
| 1.234,6 |
1234.5678 | en_US |
| 1,234.57 |
1234.5678 | fr_CA |
| 1 234,57 |
1234.5678 | en_US |
| 1235 |
1234.5678 | en_US |
| 1,235 |
1234.5678 | en_US |
| 1,234.567800 |
Notice how the locale determines which characters are printed as separators. For example, the locale en_US (English, United States) uses a period for the the decimal separator and a comma for thousand separators, whereas the locale de_DE (German, Germany) uses a comma for the decimal separator and a period for thousand separators. The number format pattern determines where the separators appear.
Formatting the currency unit
Each project has a default currency format pattern, which you set in the Settings view (Settings > Number and Currency). The pattern is a default value (third parameter) for the currencyFormat function.
Currency format patterns are also defined using JavaDecimalFormat syntax. They differ from number format patterns in one respect: they use the special character ¤
(also known as a currency sign) to define the position and formatting of the currency unit.
You can format a currency unit as its currency symbol (e.g. $) or its ISO code (e.g. USD). A single currency sign in your format pattern displays the currency symbol; a double currency sign displays the code. This is demonstrated below:
Unformatted Number | Locale | Currency | Currency Format Pattern | Output |
---|---|---|---|---|
1234 | en_US | USD |
| $1,234.00 |
1234 | en_US | USD |
| USD1,234.00 |
1234 | en_US | USD |
| 1,234.00 $ |
1234 | en_US | USD |
| 1,234.00 USD |
Positive and negative subpatterns
Your number format pattern can contain both a positive and negative subpattern. This allows you to define specific prefix and suffix characters for displaying negative numbers. The special character ;
separates positive and negative subpatterns.
For example, the pattern #,##0.##;(#,##0.##)
contains two subpatterns:
Subpattern | Applies To |
---|---|
| Positive numbers |
| Negative numbers |
The format pattern above ensures that a negative number is displayed in parentheses.
Including a negative subpattern is optional. If omitted, negative numbers are automatically prefixed by a minus sign, and all other formatting is taken from the positive pattern. The effects of including a negative subpattern are shown below.
Unformatted Number | Locale | Number Format Pattern | Output |
---|---|---|---|
1234.5678 | en_US |
| 1,234.6 |
-1234.5678 | en_US |
| -1,234.6 |
1234.5678 | en_US |
| 1,234.6 |
-1234.5678 | en_US |
| (1,234.6) |
1234.5678 | en_US |
| 1,234.57 |
-1234.5678 | en_US |
| (-1,234.57) |
Note
The number part of the negative subpattern should be identical to the number part of the positive subpattern. If they differ (e.g. the patterns display a different number of digits) you might get unexpected results.
Important
A currency format pattern can also contain a positive and negative subpattern. The same principles described above apply. See the currrencyFormat topic for examples.
ATL functions
Formatting functions
You can override your project's default number format pattern by using a formatting function:
Click on the links to learn more. Each function topic includes detailed parameter descriptions and examples.
Important
Any formatting applied by these functions overrides the project's default number format pattern.
Combining formatting functions
You can nest formatting functions — that is, you can make the output from one formatting function the input to another formatting function. If there are conflicts in the formatting of nested functions, the formatting applied by the 'outer' function is applied.
To demonstrate this, and to keep the ATL simple, all examples in this section use the following user-defined variable:
Variable Name | Variable Type | Data Source | Actual Value | Displayed As |
---|---|---|---|---|
myVar | NUMERIC |
| 1234.5678 | 1,234.57 |
As shown, myVar has an actual value of 1234.5678 and a display value of 1,234.57.
These examples show what happens when you use myVar as the input to another formatting function:
ATL in Script | Result | Notes |
---|---|---|
| 1,234.57 | The actual value of myVar (1234.5678) is displayed to two decimal places with trailing zeros stripped. |
| 1.2K | The actual value of myVar (1234.5678) is displayed according to choices made using abbreviateNumber. |
| 1.235 € | The actual value of myVar (1234.5678) is displayed according to choices made using currrencyFormat. |
| 1 234,57 | The actual value of myVar (1234.5678) is displayed according to choices made using numberFormat. |
| 1,234.56780 | The actual value of myVar (1234.5678) is displayed according to choices made using precision. |
Important
Notice that the outer function uses the input variable's actual value, not its display value.
Using formatted numbers with arithmetic operators
When you use a formatted number in calculations, the calculation uses the number's actual value, not its display value. For example, the ATL [[precision(123.4567,2)]]
would be displayed as 123.46 in your output text, but the number's actual value (123.4567) would be used if you included the same ATL in a calculation. This applies to all ATL arithmetic operators:
Operator | Syntax | Example Expression | Result |
---|---|---|---|
ADDITION |
|
| 13 |
SUBTRACTION |
|
| 7 |
MULTIPLICATION |
|
| 30 |
DIVISION |
|
| 3.33 |
EXPONENTIATION |
|
| 1000 |
MODULUS (REMAINDER) |
|
| 1 |
The examples in this section make use of the following user-defined variables:
Variable Name | Variable Type | Data Source | Actual Value | Displayed As |
---|---|---|---|---|
V1 | NUMERIC |
| 1234.5678 | 1,234.6 |
V2 | NUMERIC |
| 2345.6789 | 2,345.7 |
V3 | NUMERIC |
| 8765.4321 | 8,765.432 |
V4 | NUMERIC |
| 1200 | 1.2K |
V5 | NUMERIC |
| 120000 | 120K |
V6 | NUMERIC |
| 1200000 | 1.2M |
The Displayed As column above shows how the results are displayed in a project with the default locale set to en_US (English, United States) and the default number format pattern set to #,##0.##
.
The examples below assume the same project default settings.
ATL in Script | Actual Result | Displayed Result | Notes |
---|---|---|---|
| 3,580.2467 | 3,580.2 | The formatting applied to V1 and V2 is identical, so the result is displayed using that same formatting. |
| 11,111.111 | 11,111.11 | The formatting applied to V2 and V3 is different, so the result is displayed using the project's default number format pattern (#,##0.##). |
| 118,800 | 118.8K | The formatting applied to V5 and V4 is identical, so the result is displayed using that same formatting. |
| 1,080,000 | 1,080,000 | The formatting applied to V6 and V5 is different, so the result is displayed using the project's default number format pattern (#,##0.##). |
Important
The result is displayed using the project's default number format pattern unless all formatted numbers used in the calculation are formatted identically, in which case the result is displayed using that same formatting.
Using formatted numbers with mathematical functions
You can also perform calculations using in-built mathematical functions. This section shows how these work with numbers/variables to which formatting is already applied.
These examples make use of the following user-defined variables:
Variable Name | Variable Type | Data Source | Actual Value | Displayed As |
---|---|---|---|---|
V7 | NUMERIC |
| 1000 | 1,000 |
V8 | NUMERIC |
| 2000 | 2,000 |
V9 | NUMERIC |
| 2500 | 2,500.000 |
The Displayed As column above shows how the results are displayed in a project with the default locale set to en_US (English, United States) and the default number format pattern set to #,##0.00
.
The examples below assume the same project default settings.
ATL in Script | Actual Result | Displayed Result | Notes |
---|---|---|---|
| 3,000 | 3,000 | The formatting applied to V7 and V8 is identical, so the result is displayed using that same formatting. |
| 4,500 | 4,500.00 | The formatting applied to V8 and V9 is different, so the result is displayed using the project's default number format pattern (#,###.00). |
| 1,000 | 1,000 | The formatting applied to V8 and V7 is identical, so the result is displayed using that same formatting. |
| 500 | 500.00 | The formatting applied to V8 and V9 is different, so the result is displayed using the project's default number format pattern (#,###.00). |
Important
The result is displayed using the project's default number format pattern unless all formatted numbers used in the calculation are formatted identically, in which case the result is displayed using that same formatting.
Functions with optional DECIMAL PLACES and STRIP TRAILING ZEROS parameters
Some mathematical functions have optional DECIMAL PLACES and STRIP TRAILING ZEROS parameters. When these optional parameters are used, they supersede the values from any previous or default formatting. For example:
ATL in Script | Displayed Result |
---|---|
| 50.0 |
The DECIMAL PLACES parameter is set to 1 and the STRIP TRAILING ZEROS parameter is set to false. These parameter values take precedence over all other number formatting, so the result is displayed to one decimal place with the trailing zero retained.