Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Number Formatting (1/2) >

Function format-number

Numbers can be formatted for output using format-number() function. The first argument to this function is the number to be formatted the second one is a picture string, which provides a recipe for the formatting. In the first example the picture string indicates that the number should have 2 digits after decimal point.

Digits are indicated either by "#" or "0" characters. Please note the difference. If zeroes are used than the string is padded by 0 to the specified length.

XSLT

      <xsl:stylesheet
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  version="2.0">
            <xsl:output  method="xml"
                        indent="yes"
                        omit-xml-declaration="yes"/>

            <xsl:template  match="/aaa">
                  <aaa>
                        <xsl:value-of  select="format-number(123456789.6543,'#.##')"/>
                  </aaa>
                  <bbb>
                        <xsl:value-of  select="format-number(123456789,'#.##')"/>
                  </bbb>
                  <ccc>
                        <xsl:value-of  select="format-number(123456789,'#.00')"/>
                  </ccc>
                  <ddd>
                        <xsl:value-of  select="format-number(123456.789,'#.00')"/>
                  </ddd>
                  <eee>
                        <xsl:value-of  select="format-number(12.789,'00000.0')"/>
                  </eee>
                  <fff>
                        <xsl:value-of  select="format-number(12.789,'#####.#')"/>
                  </fff>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa/>
Output

      <aaa>123456789.65</aaa>
      <bbb>123456789</bbb>
      <ccc>123456789.00</ccc>
      <ddd>123456.79</ddd>
      <eee>00012.8</eee>
      <fff>12.8</fff>


Previous chapter: Transforming strings with regular expressions
Next chapter: Sorting
Previous page: - - -
Next page: Grouping separator