Custom Numeric Format Strings

7 Jun 20232 minutes to read

Create a custom numeric format string that contains one or more custom numeric specifiers. This numeric specifier helps to defines the format of numeric data. This type of format string is not a standard numeric format string and it can be of any format string.
Some of the custom numeric format specifiers are illustrated in the following sections.

Custom specifier “0”

The Zero-placeholder symbols fills the missing digits in “0” custom specifier. If there is a digit in the formatting values it will be displayed in the result string, otherwise 0 will replaces the digits according to the format string.

Please find some of the example formats below,

Format

Output

ej.format(123, "00000"); 00123
ej.format(1.2, "00.00"); 01.20
ej.format(1.2, "0.00"); 1.20
ej.format(1.2, "00.00", "de-DE") 1,20
ej.format(0.56, "0.0") 0.6
ej.format(1234567890, "0,0") 1,234,567,890
ej.format(1234567890.123456, "0,0.0") 1,234,567,890.1
ej.format(1234.567890, "0,0.00") 1, 234.57

Custom specifier “#”

Formatting values contains digits in the place of “#” symbol in the format string, it will be displayed in the result string. Otherwise none will be placed in the position of result string.
It never allowed zero unless it is a significant digit in the formatting values.

Please find some of the example formats below,

Format

Output

ej.format(1.2, "#.##") 1.2
ej.format(123, "#####") 123
ej.format(123456, "[##-##-##]") [12-34-56]
ej.format(1234567890, "#") 1234567890
ej.format(1234567890, "(###) ###-####") (123) 456-7890
ej.format(0.324, "#.###") 0.324
ej.format(123456789, "#,#") 123,456,789

Custom specifier “Exponential”

The strings such as “E”, “E+”, “E-“, “e”, “e+”, or “e-“ is presents in the format string, then the number is formatted by scientific notation with “E” or “e” inserted in between the number and the exponent.  The number of zeros following the scientific notation indicator regulates the minimum number of digits to the output of the exponent. 
The plus sign or minus sign is always come with the exponent symbol. The following are the formats used in customer specifier “exponential”:

  • “E+” and “e+”:  a plus sign preceding the exponent.
  • “E”, “E-“, “e”, or “e-“:  Precede only negative exponents.

Please find some of the example formats below,

Format

Output

ej.format(123456, "#.##E+2") 1.23e+2
ej.format(123456, "#.##E+002") 1.23e+052
ej.format(123456, "#.##E-2") 1.23e2
ej.format(123456, "#.##E-002") 1.23e052
ej.format(1634.92, "#.##E+4") 1.63e+4
ej.format(1634.92, "#.##E+004") 1.63e+034
ej.format(1634.92, "#.##E-2") 1.63e2
ej.format(1634.92, "#.##E-002") 1.63e032
ej.format(86000, "#.##E+000") 8.6e+004

Custom specifier “Escape”

The symbols “#”, “0”, “.”, “,”, “E” in a format string are consider as format specifiers rather than as literal characters. To prevent these characters from being considered as format specifier, precede it with a back slash ”\” that is the escape character.  The escape character highlights that the following character is a character literal and should be included in the result string without any change. To include a backslash in a result string, escape it with another backslash (\).

Please find some of the example formats below,

Format

Output

ej.format(123, "\\#\\#\\ #00") ##123
ej.format(123, "\\#\\#\\ ###") ##123
ej.format(123, "\\#\\#\\ 000") ##123
ej.format(123, "\\0\\0 000") 00123
ej.format(12345, "\\#\\#\\ 0,0") ##12,345
ej.format(12345, "\\0\\0 #,#") 0012,345