XSLT » Functions » format-number

Syntax:
string = format-number(number, format, decimal-format-qname>)
decimal-format-qname
The optional decimal-format-name argument is a qname that matches the qname value of the name attribute of the xsl:decimal-format element. The xsl:decimal-format element must be present to use this argument. If this argument is omitted, the default decimal format is used. However, you can create your own default decimal format by using the xsl:decimal-format element without a name attribute.
format
The mandatory format argument specifies the format pattern that dictates the appearance of the formatted number. Some of the permitted symbols in the format pattern are:
SymbolPurposeExample
# signifies a digit [0-9] ###
. decimal point ###.##
, group separator ###,###.##
0 specifies leading and following zeros 00000.00000
% inserts following percent sign ##%
; pattern separator ##.00;##.00
number
The mandatory number argument is the number that you want converted to a string. If the value is not of type number, it will automatically be converted to a number by the XPath number() function.

The format-number function is used to convert a single number into a string. This number can be a real number or an integer, and can be positive or negative.

Note that the xsl:number element can be used to format a list of positive integers.

Examples

Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:decimal-format name="staff" digit="D" />
<xsl:template match="/">
<html>
<body>
<xsl:value-of select='format-number(123456789, "#.000000000")' />
<br />
<xsl:value-of select='format-number(123456789, "#.0")' />
<br />
<xsl:value-of select='format-number(0.123456789, "##%")' />
<br />
<xsl:value-of select='format-number(123456789, "################")' />
<br />
<xsl:value-of select='format-number(123456789, "D.0", "staff")' />
<br />
<xsl:value-of select='format-number(123456789, "$DDD,DDD,DDD.DD", "staff")' />
<br />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
123456789.000000000
123456789.0
12%
123456789
123456789.0
$123,456,789
Explanation:

Here, we display several numeric format variations. This is the code for xslt_example_formatnumber.xsl.

Language(s): XSLT

See Also: