Using Formatting APIs to Format Numbers and Dates

As an application designer, you can write scripts to allow end users to format certain existing number values and date values in an application.

The format options for these two types of values are as follows:

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 enabled, 123456 will be turned into 123,456

  • Set separator of the digit grouping

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

  • Display sign value and set format of the sign value

    Example: In a normal case, “+” and “-” can be set as the format of the sign value. 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.

Date Values

  • Set the pattern of the date

    Example: 2016/01/10 will be turned into Jan 10, 2016 if you set a date pattern in MMM dd, yyyy format.

For detailed information about all related APIs, refer to the Analytics Designer API Reference.

Example

If you want to allow end users to click a button and display the formatted current system date in a text widget Text_1, write the script below for the button:

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

If you want to allow the end users to click a button and format the digit grouping of a number input in the widget InputField_1 according to the user preference settings, write the script below:

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

If you want to allow end users to click a button and format the digit grouping of a number input in the widget InputField_1 according to a customized setting, write the script below:

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));

The new format will set the digit grouping separator as “.” and the decimal separator as “,” and the formatted number will be displayed in the widget Text_1.