ng:numberFormat

ng:numberFormat

This section describes the built-in function: ng:numberFormat(number, locale, mask, customErrorMessage)

The function can be used for presenting/formatting a number as you want.

 

Important note regarding rounding of numbers in InterFormNG2 versions prior to version 2.1.1:

(In version 2.1.1 and newer normal rounding - to nearest digit is used)

The default number format use bankers rounding (in older versions than 2.1.1), which rounds 0.5 in the direction of nearest even number.

 

This will be changed in a future version of InterFormNG2. Until then the standard XPath round method can be used:

 

Examples:

ng:numberFormat(0.01*round(100*2.005), "en-US", "#0.00")

for 0.00 formats

 

or just

ng:numberFormat(round(2.5), "en-US", "#0")

- for removing numbers after the decimal point.

 

The function returns a formatted string representation of the number. Exactly two fractional digits will be shown when mask is not used.

 

Please notice

Numbers formatted with Swedish and Finnish locale in PDF, that are using non-breakable space are using a space instead of this as many fonts does not include the non-breaking space. You need yourself to ensure, that a number is not accidentally wrapped around.

 

An alternative is the standard XPath function, format-number().

 

The function has these parameters:

 

number

A valid number in the format: No thousand separator and the optional decimal point must be dot. If the input is empty (null), then the output is empty (null). The number can also be provided as a string.

To recognize a number as numeric the format of the data must be -###.## i.e. no thousand separator and a dot as decimal point and a preceding minus for negative numbers.

So valid numbers are: '123', '12.12' and '12345.44'.

Examples of invalid numbers: '123,11' and '12,345.33'.

The result of the formatting depends (for large numbers with more than 16 digits) on the input format: If the input number is provided as a number, then the result is limited to 16 digits (and the lower significant digits will be rounded off to 0), but if the input number is provided as a string, then there is no limit to the number of significant digits.

 

 

locale

Optional input specifying the locale to use when creating the string. This affects for instance if dot or comma is used as decimal separator in the output. If this parameter is not specified, then the locale setup on the template settings is used if used in a template (en-GB is used by default in the workflow). Without the mask parameter below the default presentation will be the number found in parameter 1 rounded off to 2 decimals (fixed so zeroes will be added to ensure 2 decimals like for a normal amount) and a thousand separator according to the locale specified here.

Possible values for the locale can be found on the drop down list on e.g. the Translation element.

You can also find a list of locales here: https://www.oracle.com/java/technologies/javase/jdk8-jre8-suported-locales.html

 

mask

Optional mask with which custom formatting rules can be specified.

Note that in the designer result view, the result should normally display the same result as the final output when the mask is NOT used (no guarantee though, since it is different implementations). When mask is used, the mask will be shown in the result view.

 

You can use these characters as the mask:

 

0 (Digit)

# (Digit, zero shows as absent)

. (The position of the decimal point Example: ###.##)

, (The group separator for thousands. Example: ###,###.##)

% (Displays the number as a percentage. Example: ##%)

; (Pattern separator. The first pattern will be used for positive numbers and the second for negative numbers)

 

Some examples:

 

XPath

Output

ng:numberFormat(123456.6543,'da-DK')

123.456,65

ng:numberFormat(0.65, 'da-DK', '###%')

65%

ng:numberFormat(123.45, 'en-US', '$000000.00')

$000123.45

 

customErrorMessage

Optional input which specifies the value to return, if the function cannot successfully convert the input number to the requested format (e.g. if the first parameter is not a number). If not specified InterFormNG2 will return the Xpath specifications.

 

 

Examples

 

Round off to 2 fixed digits

If you want to round off a number to 2 digits (and insert zeroes to always have two digits e.g. for an amount), then you can use this expression:

 

ng:numberFormat(12345.67890, 'en-US', '#,###.00')

 

This expression output this number

12,345.68

 

This expression:

ng:numberFormat(12345.6, 'en-US', '#,###.00')

 

outputs the number: 12,345.60

 

 

 

Present number in e.g. Swiss format with single quote as thousand separator.

The swiss format for numbers is like so: 1'234.56, where the thousand separator is a single quote. It may be a problem to refer to that single quote when you want to specify a mask for that.

In general you can freely switch between duoble quote (") and single quote (') to delimit a string in XPath, but that unfortunately does not work for the mask in the ng:numberFormat() function, so this expression does not work:

NG2ngnumberFormat0005

 

For amounts you should instead consider this simple expression:

 

ng:numberFormat(123456.6543,'de_CH')

This will round off the number in the first parameter in the swiss format and always present the number with 2 decimals and the single quote as thousand separator:

 

NG2ngnumberFormat0006

    • Related Articles

    • ng:resourceExist

      ng:resourceExist(resource,path) This function checks, if a resource in InterFormNG2 exists or not. The function returns a boolean: true or false. The ng:resourceExist Xpath function has this format: ng:resourceExist(resource,path), where: resource ...
    • ng:decrypt

      The built-in function, ng:decrypt is linked with the encryption functions: ng:encrypt and the workflow component, Set secure workflow variable. The function, ng:decrypt can decrypt contents, that was previously encrypted with these functions. The ...
    • ng:minimum

      InterFormNG2 includes the special built-in XPath function, ng:minimum. This function returns the smallest value of two numeric arguments. The function only accepts two numeric arguments - no more and no less. Examples: ng:minimum(-14,2) returns 2 ...
    • ng:base64

      The built-in function, ng:base64 converts a string (text) into a base64 based string. This can e.g. be used, if you want to insert any kind of data into an XML node, without substituting any characters, that are not allowed. This function is related ...
    • ng:fileExist

      ng:fileExist(path) The ng:fileExist Xpath function has this format: ng:fileExist(path), where path contains the path to a file, that should be verified. The function returns true, if the file exists and false, if the file does not exist. The function ...