The format-number () function of XSLT is really powerful in formatting numbers. Simply specifying a format string can format numbers into any desired format. However, a problem occurs recently. If a number is formatted as a non-numeric string, the output result is a nan string, what I want is to display the "-" character. Before formatting, do you need to determine whether it is Nan? The condition-based judgment Statement of XSLT is too troublesome. It is very complicated to judge every formatting. There is no other way. I checked www.w3cshool.com, And the <XSL: decimal-format> element matches the format-number.
<XSL: decimal-format name = "name" Decimal-separator = "char" grouping-separator = "char" infinity = "string" minus-Sign = "char" Nan = "string" percent = "char" digit = "char"
Per-Mille = "char" zero-digit = "char" pattern-separator = "char"/>
<XSL: decimal-format> defines some characters used to convert a number into a string using the format-number () function. Use the <XSL: decimal-format> element to modify the preceding special characters. For example, in Europe, a comma is used to indicate the decimal point. The <XSL: decimal-format> element can be used.
In addition to setting a global format, you can also define an <XSL: decimal-format> element with the name attribute. The format-number () function can call a specific element through name, the format-number call in other places is not affected.
The following describes attributes in detail.
Attribute |
Value |
Description |
Name |
String |
Optional. Specifies the name of the format. If this attribute is set, the format-number () function can load a specific format through name. If this attribute is not specified, the format is used as the default format for all format-numbers. For example: <XSL: decimal-format name = "Euro" digit = "D"/> <XSL: value-of select = 'format-number (12.033, "dd.0000", "Euro") '/> <Br/> <XSL: value-of select = 'format-number (12.033, "#. 0000") '/> Output: 12.033 12.033 |
Decimal-separator |
Char |
Optional. Indicates the decimal point. The default value is "." For example: <XSL: decimal-format Decimal-separator = ","/> <XSL: value-of select = 'format-number (12.033, "000,000") '/> Output: 012,033 The comma here is the decimal point. |
Grouping-separator |
Char |
Optional. Specify the thousands separator. The default value is "," For example: <XSL: decimal-format grouping-separator = "*"/> <XSL: value-of select = 'format-number (1234567890, "#########") '/> Output: 1*234*567*890 |
Infinity |
String |
Optional. Indicates an infinite string. The default value is "infinity" For example: <XSL: decimal-format infinity = "infinity"/> <XSL: value-of select = 'format-number (1 Div 0, "0.0") '/> Output: Infinity |
Minus-sign |
Char |
Optional. Specifies the negative character. The default value is "-" For example: <XSL: decimal-format minus-Sign = "+"/> <XSL: value-of select = 'format-number (-102, "0.0") '/> Output: + 102.0 Do you still need the ABS function? convert the output directly. |
Nan |
String |
Optional. Specifies a non-numeric string. The default value is "Nan" For example: <XSL: decimal-format Nan = "non-numeric"/> <XSL: value-of select = 'format-number (a, "0.0") '/> Output: Non-numeric value |
Percent |
Char |
Optional. Percent sign. The default value is "%" For example: <XSL: decimal-format percent = "^"/> <XSL: value-of select = 'format-number (12, "0.0 ^") '/> Output: 1200.0 ^ |
Per-Mille |
Char |
Optional. Specify a semicolon. The default value is "‰" For example: <XSL: decimal-format per-Mille = "^"/> <XSL: value-of select = 'format-number (12, "0.0 ^") '/> Output: 12000.0 ^ |
Zero-digit |
Char |
Optional. Specifies a string that represents the number 0. The default value is "0" For example: <XSL: decimal-format zero-digit = "2"/> <XSL: value-of select = 'format-number (123, "#. #") '/> Output: 345 |
Digit |
Char |
Optional. A number is required for the character in the specified format. The default value is # For example: <XSL: decimal-format digit = "D"/> <XSL: value-of select = 'format-number (12.3, "D. D") '/> Output: 12.3 |
Pattern-separator |
Char |
Optional. The delimiter of the positive and negative seed modes in the specified format. The default value is ";" For example: <XSL: decimal-format pattern-separator = ";"/> <XSL: value-of select = 'format-number (123, "-000; + 000") '/> <XSL: value-of select = 'format-number (-123, "-000; + 000") '/> Output: -123 + 123 |
|
Char in the "value" column in the preceding table indicates that the attribute is a character, and string indicates that the attribute is a string. With the <XSL: decimal-format> element, will it still fall into the quagmire of Concatenated strings?