Use Formatting APIs to Format Numbers and Dates

You can write scripts in analytic applications and optimized stories to let viewers format the existing number values and date values.

There're following format options for these two types of values:

  • number values

    • Set maximum decimal places

      Example: 1.23456 will be turned into 1.234 if the maximum decimal place is set as 3.

    • Set minimum decimal places

      Example: 1.23 will be turned into 1.230 if the minimum decimal place is set as 3.

    • Set the decimal separator

      Example: 1.23 will be turned into 1 23 if the decimal separator is set as empty space.

    • Enable digit grouping

      Example: If digital grouping is enabled, 123456 will be turned into 123,456.

    • Set separator of digit grouping

      Example: 123,456 will be turned into 123.456, if dot is set as the separator of the digital grouping.

    • Display sign and set sign format

      Example: In a normal case, “+” and “-” can be set as the sign format. In some special cases, “()” can be set to indicate negative value too.

    • Display scale and set scale multiplier and affix

      Example: E can be used as the scale multiplier, such as 1E-3; M can be set as a scale affix so that 123,456,789 can be set as 123M.

    Note

    Number format APIs only apply to measures on axes, Feed.ValueAxis and Feed.ValueAxis2. Tooltip measures on axes, Feed.TooltipValueAxis, aren’t supported, for example.

  • date values

    Set date format: For example, 2016/01/10 will be turned into Jan 10, 2016 if you set a date pattern in MM dd, yyyy format.

For detailed information about all related APIs, refer to Analytics Designer API Reference (for analytic applications) and Optimized Story Experience API Reference (for optimized stories).

Example

You'd like to let end users view the formatted current system date in a text widget Text_1 via clicking a button. Write the following script for the button:

Sample Code
var s = DateFormat.format(new Date(), "MM dd, yyyy");
Text_1.applyText(s);
Example

You'd like to let end users format the digit grouping of a number input in the widget InputField_1 according to their user preference settings via clicking a button. Write the following script for the button:

Sample Code
var s = InputField_1.getValue();
var n = Number.parseFloat(s);
Text_1.applyText(NumberFormat.format(n));

The formatted number will be displayed in the widget Text_1.

Example

You'd like to let end users format the digit grouping of a number input in the widget InputField_1 according to your customized setting. Here's the script for the button:

Sample Code
var s = InputField_1.getValue();
var n = Number.parseFloat(s);
var nf = NumberFormat.create();
nf.setMinimumDecimalPlaces(3);
nf.setGroupingSeparator(".");
nf.setDecimalSeparator(",");
Text_1.applyText(nf.format(n));

In this case, the new format will apply the digit grouping separator “.” and the decimal separator “,” and the formatted number will be displayed in the widget Text_1.