format-number() Function |
Takes a number and formats it as a string.
Inputs
The number to be formatted and the format pattern string are required. The third argument is the optional name of a decimal format; if the third argument is not supplied, the default decimal format is used.
Output
The number, formatted according to the rules supplied by the other arguments. The special characters used in the second argument are:

#
  Represents a digit. Trailing or leading zeroes are not displayed. Formatting the number 4.0 with the string " #.## " returns the string " 4 " .

0
  Represents a digit. Unlike the # character, the 0 always displays a zero. Formatting the number 4.1 with the string " #.00 " returns the string " 4.10 " .

.
  Represents the decimal point.

-
  Represents the minus sign.

,
  Is the grouping separator.

;
  Separates the positive-number pattern from the negative-number pattern.

%
  Indicates that a number should be displayed as a percentage. The value will be multiplied by 100, then displayed as a percentage. Formatting the number .76 with the string " ##% " returns the string " 76% " .

\u2030
  Is the Unicode character for the per-thousand (per-mille) sign. The value will be multiplied by 1000, then displayed as a per mille. Formatting the number .768 with the string " ###\u2030 " returns the string " 768 " .

The third argument, if given, must be the name of an <xsl:decimal-format> element. The <xsl:decimal-format> element lets you define the character that should be used for the decimal point and the grouping separator, the string used to represent infinity, and other formatting options. See <xsl:decimal-format> for more information.

Defined in
XSLT section 12.3, Number Formatting.
Example
The following stylesheet uses the format-number() function in various ways:
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:months="Lookup table for month names">

      <xsl:output method="text"/>

      <months:name sequence="01">January</months:name>
      <months:name sequence="02">February</months:name>
      <months:name sequence="03">March</months:name>
      <months:name sequence="04">April</months:name>

      <xsl:variable name="newline">
    <xsl:text>
    </xsl:text>
      </xsl:variable>

      <xsl:decimal-format name="f1"
        decimal-separator=":"
        grouping-separator="/"/>

      <xsl:decimal-format name="f2"
        infinity="Really, really big"
        NaN="[not a number]"/>

      <xsl:template match="/">
        <xsl:value-of select="$newline"/>
        <xsl:text>Tests of the format-number() function:</xsl:text>

        <xsl:value-of select="$newline"/>
        <xsl:value-of select="$newline"/>
        <xsl:text>   format-number(528.3, '#.#;-#.#')=</xsl:text>
        <xsl:value-of select="format-number(528.3, '#.#;-#.#')"/>
        <xsl:value-of select="$newline"/>
        <xsl:text>   format-number(528.3, '0,000.00;-0,000.00')=</xsl:text>
        <xsl:value-of select="format-number(528.3, '0,000.00;-0,000.00')"/>
        <xsl:value-of select="$newline"/>
        <xsl:text>   format-number(-23528.3, '$#,###.00;($#,###.00)')=</xsl:text>
        <xsl:value-of select="format-number(-23528.3, '$#,###.00;($#,###.00)')"/>
        <xsl:value-of select="$newline"/>
        <xsl:text>   format-number(1528.3, '#/###:00', 'f1')=</xsl:text>
        <xsl:value-of select="format-number(1528.3, '#/###:00;-#/###:00', 'f1')"/>
        <xsl:value-of select="$newline"/>
        <xsl:text>   format-number(1 div 0, '###,###.00', 'f2')=</xsl:text>
        <xsl:value-of select="format-number(1 div 0, '###,###.00', 'f2')"/>
        <xsl:value-of select="$newline"/>
        <xsl:text>   format-number(blue div orange, '#.##', 'f2')=</xsl:text>
        <xsl:value-of select="format-number(blue div orange, '#.##', 'f2')"/>
        <xsl:value-of select="$newline"/>
        <xsl:value-of select="$newline"/>
        <xsl:for-each select="report/month">
          <xsl:text>   </xsl:text>
          <xsl:value-of
            select="document('')/*/months:name[@sequence=current()/@sequence]"/>
          <xsl:text> - </xsl:text>
          <xsl:value-of select="format-number(miles-flown, '##,###')"/>
          <xsl:text> miles flown, </xsl:text>
          <xsl:value-of select="format-number(miles-earned, '##,###')"/>
          <xsl:text> miles earned.</xsl:text>
          <xsl:value-of select="$newline"/>
          <xsl:text>     (</xsl:text>
          <xsl:value-of
            select="format-number(miles-flown div sum(//miles-flown), '##%')"/>
          <xsl:text> of all miles flown, </xsl:text>
          <xsl:value-of
            select="format-number(miles-earned div sum(//miles-earned), '##%')"/>
          <xsl:text> of all miles earned.)</xsl:text>
          <xsl:value-of select="$newline"/>
          <xsl:value-of select="$newline"/>
        </xsl:for-each>
        <xsl:text>   Total miles flown: </xsl:text>
        <xsl:value-of select="format-number(sum(//miles-flown), '##,###')"/>
        <xsl:text>, total miles earned: </xsl:text>
        <xsl:value-of select="format-number(sum(//miles-earned), '##,###')"/>
      </xsl:template>

    </xsl:stylesheet>
We'll use this XML document with our stylesheet:
    <?xml version="1.0"?>
    <report>
      <title>Miles Flown in 2001</title>
      <month sequence="01">
        <miles-flown>12379</miles-flown>
        <miles-earned>35215</miles-earned>
      </month>
      <month sequence="02">
        <miles-flown>32857</miles-flown>
        <miles-earned>92731</miles-earned>
      </month>
      <month sequence="03">
        <miles-flown>19920</miles-flown>
        <miles-earned>76725</miles-earned>
      </month>
      <month sequence="04">
        <miles-flown>18903</miles-flown>
        <miles-earned>31781</miles-earned>
      </month>
    </report>
When we run this stylesheet, here are the results:
    Tests of the format-number() function:

       format-number(528.3, '#.#;-#.#')=528.3
       format-number(528.3, '0,000.00;-0,000.00')=0,528.30
       format-number(-23528.3, '$#,###.00;($#,###.00)')=($23,528.30)
       format-number(1528.3, '#/###:00', 'f1')=1/528:30
       format-number(1 div 0, '###,###.00', 'f2')=Really, really big
       format-number(blue div orange, '#.##', 'f2')=[not a number]

       January - 12,379 miles flown, 35,215 miles earned.
         (15% of all miles flown, 15% of all miles earned.)

       February - 32,857 miles flown, 92,731 miles earned.
         (39% of all miles flown, 39% of all miles earned.)

       March - 19,920 miles flown, 76,725 miles earned.
         (24% of all miles flown, 32% of all miles earned.)

       April - 18,903 miles flown, 31,781 miles earned.
         (22% of all miles flown, 13% of all miles earned.)

       Total miles flown: 84,059, total miles earned: 236,452
The first few examples illustrate some of the more complicated formatting options available, along with references to the <xsl:decimal-format> elements in the stylesheet. The last section is a more typical use of the format-number function: formatting values selected or calculated from an XML document.