Articles in this section

How to format a number input using FormatNumber()

This article explains what the FormatNumber() function is and provide examples of how it can be used with the binding syntax in smart templates.

  Prerequisites

  • Library and at least one Dynamics module enabled.
  • Templafy Desktop and Office VSTO add-in installed on the PC.
  • Space owner access to the Templafy tenant.

What is the FormatNumber() function?

FormatNumber() is a function that formats numbers. This function is only applicable to number or currency objects, these can either originate from a number question in the response form, from a number based on a schema field in a data source, or from a value of type number or currency in an external system using an App Connector or Data Connector.

FormatNumber() logic

Syntax {FormatNumber(Number,Format,Language)}}
Input Number, text, text
Output Number formatted according to format and language

  Important

  • The syntax {{FormatNumber(Number,Format)}} can also be used.
    It will use the 'DocumentLanguage' set in the User Profile.
  • FormatNumber() only applies to number objects and is not supported for any other object type.

FormatNumber() function examples

In the examples below the form field 'Amount' is used. This is a dropdown field that is linked to the Price column of the Products data source:

formatnumberdropdown.png

FormatNumber() function examples using standard numeric format strings

Currency (C or c)

In the examples below the 'DocumentLanguage' in the User Profile set to "en-US".

Binding Input Output
{{FormatNumber(Form.Amount.Price, "C", "da-DK")}} 1234 1.234,00 kr.
{{FormatNumber(Form.Amount.Price, "C", "nl-NL")}} 1234 € 1.234,00
{{FormatNumber(Form.Amount.Price, "C", "nl-NL")}} -1234 € -1.234,00
{{FormatNumber(HostSystem.Amount, "C", "nl-NL")}} 1234 € 1.234,00
{{FormatNumber(Form.Amount.Price, "C", DocumentLanguage)}} 1234 $1,234.00
{{FormatNumber(Form.Amount.Price, "C")}} 1234 $1,234.00

  Note

In the last example the syntax {{FormatNumber(Number,Format)}} is used.
It uses the 'DocumentLanguage' set in the User Profile.

Exponential (E or e)

For exponential notation, the default value of the precision specifier (number of decimal digits) is 6.
You can add a number behind the E (e.g. "2") to change the value of the precision specifier.

Binding Input Output
{{FormatNumber(Form.Amount.Price, "E", "en-US")}}  1234 1.234000E+003
{{FormatNumber(Form.Amount.Price, "E2", "en-US")}}  1234 1.23E+003

Fixed-Point (F or f)

Fixed-Point returns integral and decimal digits with an optional negative sign.
You can add a number behind the E (e.g. "1") to change the value of the precision specifier.

Binding Input Output
{{FormatNumber(Form.Amount.Price, "F", "en-US")}} 1234 1234.000
{{FormatNumber(Form.Amount.Price, "F1", "en-US")}} 1234 1234.0

General (G or g)

General returns the more compact of either fixed-point or scientific notation.
You can add a number behind the G (e.g. "4") to change the value of the precision specifier.

Binding Input Output
{{FormatNumber(Form.Amount.Price, "G", "en-US")}} 1234 1234
{{FormatNumber(Form.Amount.Price, "G4", "en-US")}} 1234 1234
{{FormatNumber(HostSystem.Amount, "G", "nl-NL")}} 123.456 123,456
{{FormatNumber(HostSystem.Amount, "G4", "en-US")}} 123.4546 123.5

  Note

A value like "123.456" shown above (containing a decimal separator) can be used in a data source (number) column and also in a HostSystem binding.

Number (N or n)

Number returns integral and decimal digits, group separators, and a decimal separator with an optional negative sign.
You can add a number behind the N (e.g. "1" or "3") to change the value of the precision specifier.

Binding Input Output
{{FormatNumber(Form.Amount.Price, "N", "en-US")}} 1234 1,234.000
{{FormatNumber(Form.Amount.Price, "N1", "en-US")}} 1234 1,234.0
{{FormatNumber(HostSystem.Amount, "N", "en-US")}} 1234.567 1,234.57
{{FormatNumber(HostSystem.Amount, "N3", "en-US")}} -1234.56 -1,234.560

  Note

A value like "1234.567" shown above (containing a decimal separator) can be used in a data source (number) column and also in a HostSystem binding.

Percentage (P or p)

Percentage returns a number multiplied by 100 and displayed with a percent symbol.

Note: in these examples "Form.Amount.Price" is replaced by 'Form.Percentage.Value', and "HostSystem.Amount" by "HostSystem.Percentage".

Binding Input Output
{{FormatNumber(Form.Percentage.Value, "P", "en-US")}} 1 100.00 %
{{FormatNumber(Form.Percentage.Value, "P", "en-US")}} 1234 123,400.000%
{{FormatNumber(Form.Percentage.Value, "P1", "en-US")}} 1234 123,400.0%
{{FormatNumber(HostSystem.Percentage, "P1", "nl-NL")}} -0.39678 -39,7 %

  Note

A value like "-0.39678" shown above (starting with a zero and containing a decimal separator) can be used in a data source (number) column and also in a HostSystem binding.

FormatNumber() function examples using custom numeric format strings

Zero placeholder (0)

Replaces a zero with the corresponding digit if one is present, otherwise a zero appears in the result string.

Binding Input Output
{{FormatNumber(Form.Amount.Price, "00000")}} 12 00012
{{FormatNumber(Form.Amount.Price, "00000")}} 123456 123456
{{FormatNumber(Form.Amount.Price, "00000")}} -1234 -01234
{{FormatNumber(HostSystem.Amount, "0.00", "en-US")}} 0.45678 0.46
{{FormatNumber(HostSystem.Amount, "0.00", "nl-NL")}} 0.45678 0,46

  Note

A value like "0.45678" shown above (starting with a zero and containing a decimal separator) can be used in a data source (number) column and also in a HostSystem binding.

Digit placeholder (#)

Digit placeholder replaces the "#" symbol with the corresponding digit if one is present, otherwise, no digit appears in the result string.
Note that no digit appears in the result string if the corresponding digit in the input string is a non-significant 0. For example, 0003 ("####") will return 3.

Binding Input Output
{{FormatNumber(Form.Amount.Price, "#####")}} 1234 1234
{{FormatNumber(HostSystem.Amount, "#####")}} 1234.5678 1235
{{FormatNumber(HostSystem.Amount, "#.##", "en-US")}} 0.45678 .46
{{FormatNumber(HostSystem.Amount, "#.##", "nl-NL")}} 0.45678 ,46

  Note

A value like "0.45678" shown above (starting with a zero and containing a decimal separator) can be used in a data source (number) column and also in a HostSystem binding.

Decimal point (.)

Decimal point determines the location of the decimal separator in the result string.

Binding Input Output
{{FormatNumber(Form.Amount.Price, "0.00", "en-US")}} 1234 1234.00
{{FormatNumber(Form.Amount.Price, "0.00", "nl-NL")}} 1234 1.234,00

Group separator and Number scaling (,)

Serves as both a group separator and a number scaling specifier. As a group separator, it inserts a localized group separator character between each group. As a number scaling specifier, it divides a number by 1000 for each comma specified.

Group separator specifier

Binding Input Output
{{FormatNumber(Form.Amount.Price, "##,#", "en-US")}} 1234567890 1,234,567,890
{{FormatNumber(Form.Amount.Price, "##,#", "nl-NL")}} 1234567890 1.234.567.890

Number separator specifier

Binding Input Output
{{FormatNumber(Form.Amount.Price, "#,#,,", "en-US")}} 1234567890 1,235
{{FormatNumber(Form.Amount.Price, "#,#,,", "nl-NL")}} 1234567890 1.235

Percentage placeholder (%)

Percentage placeholder multiplies a number by 100 and inserts a localized percentage symbol in the result string.

Note: in these examples "Form.Amount.Price" is replaced by "Form.Percentage.Value".

Binding Input Output
{{FormatNumber(Form.Percentage.Value, "%#0.00", "en-US")}} 12 %1200.00
{{FormatNumber(Form.Percentage.Value, "##.0 %", "en-US")}} 12 1200.0 %
{{FormatNumber(Form.Percentage.Value, "##.0 %", "nl-NL")}} 12 1200,0 %

Per mille placeholder (‰)

Per mille placeholder multiplies a number by 1000 and inserts a localized per mille symbol in the result string.

Note: in these examples "Form.Amount.Price" is replaced by "Form.Permille.Value".

Binding Input Output
{{FormatNumber(Form.Permille.Value, "#0.00‰", "en-US")}} 12 12000.00‰
{{FormatNumber(Form.Permille.Value, "#0.00‰", "nl-NL")}} 12 12000,00‰

Escape character (\)

Escape character causes the next character to be interpreted as a literal rather than as a custom format specifier.

Binding Input Output
{{FormatNumber(Form.Amount.Price, "\###00\#")}} 1234 #1234#

Literal string delimiter (string)

Literal string delimiter indicates that the enclosed characters should be copied to the result string unchanged.

Note: in these examples 'Form.Amount.Price' is replaced by "Form.Angle.Value".

Binding Input Output
{{FormatNumber(Form.Angle.Value, "# 'degrees'")}} 12 12 degrees
{{FormatNumber(Form.Angle.Value, "#' degrees'")}} 12 12 degrees

Section separator (;)

Section separator defines sections with separate format strings for positive, negative, and zero numbers.

Binding Input Output
{{FormatNumber(Form.Amount.Price, "#0.0#;(#0.0#);-\0-")}} 1234 1234,0
{{FormatNumber(Form.Amount.Price, "#0.0#;(#0.0#);-\0-")}} 0 -0-
{{FormatNumber(Form.Amount.Price, "#0.0#;(#0.0#);-\0-")}} -1234 (1234,0)

All other characters

All other characters: the character is copied to the result string unchanged.

Note: in this example "Form.Amount.Price" is replaced by "Form.Angle.Value".

Binding Input Output
{{FormatNumber(Form.Angle.Value, "# °")}} 12 12 °

  Important

For more info see https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings and https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings. Note that the format specifiers 'D' (decimal), 'R' (round-trip) and 'X' (hexadecimal) mentioned on these pages are not supported by Templafy.

 

translations templates spreadsheet theme ticks stringjoin today's date tip of the day fixed-point translate true translation stringsplit tech_role
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.