June 2008
Intermediate to advanced
986 pages
27h 8m
English
We’ve already seen several ways of formatting decimal
numbers using <xsl:number>.
However, if we’re going to work with numbers, we’ll almost certainly
have to deal with decimals. XSLT defines the format-number() function and the <xsl:decimal-format> element to do just that. We’ll use <xsl:decimal-format> to define a pattern
for formatting numbers, and then we’ll use format-number() to apply a pattern to a
number.
This stylesheet has several examples of <xsl:decimal-format> and format-number():
<?xml version="1.0" encoding="utf-8"?>
<!-- decimal-format.xsl --> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <!-- This format has no name, so it's assumed to be the default. --> <xsl:decimal-format decimal-separator="," grouping-separator="."/> <xsl:decimal-format name="us_default"/> <xsl:decimal-format name="other_options" NaN="[not a number]" infinity="unfathomably huge"/> <xsl:decimal-format name="hash_mark" digit="!"/> <xsl:template match="/"> <xsl:text>
Tests of <xsl:decimal-format> and </xsl:text> <xsl:text>format-number():</xsl:text> <xsl:text>

 1. format-number(3728493.3882, </xsl:text> <xsl:text>'#.###,##') : </xsl:text> <xsl:value-of select="format-number(3728493.3882, '#.###,##')"/> <xsl:text>

 2. format-number(3728493.3882, </xsl:text> <xsl:text>'#,###.##', 'us_default') : </xsl:text> <xsl:value-of select="format-number(3728493.3882, '#,###.##', 'us_default')"/> ...Read now
Unlock full access