Skip to content

XSLT format-number() Function

How to use the XSLT format-number() function to convert numbers into formatted strings with patterns for decimals, percentages, thousands separators, and custom decimal formats.

XSLT format-number() Function

The format-number function is used to convert a number into a string.

Syntax

string format-number(number,format,[decimalformat])

Input Parameters

Parameter Required/Optional Description
number Required Specifies the number to be formatted
format Required Specifies the format pattern. Here are some of the characters used in the formatting pattern:
  • 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)
decimalformat Optional. A QName that matches the qname value of the name attribute of the xsl:decimal-format element

Example

example.xslxml
<?xml version="1.0"  encoding="UTF-8"?>
<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>

And the output is:

123456789.000000000  
123456789.0  
12%  
123456789  
123456789.0  
$123,456,789
Navigation