XML technologies / XSLT / XSLT Cookbook / Numbers and calculations / Round numbers to a specified precision
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You want a specified number of decimal places, but XSLT functions round, ceiling and floor always produce integers.
Solution
XSLT 1.0
Multiply, round and divide by a power of ten determining the required decimal places. Suppose $pi = 3,1415926535897932
<xsl:value-of select="round($pi * 10000) div 10000"/>gives 3,1416. Similarly,
<xsl:value-of select="ceiling($pi * 10000) div 10000"/>gives 3,1416 and
<xsl:value-of select="floor($pi * 10000) div 10000"/>gives 3,1415.
Decimal rounding can also use format-number( ):
<xsl:value-of select="format-number($pi,'#.####')"/>The result is 3,1416. This works with multiple significant integer digits because format-number never uses a format specification to remove significant integer digits:
<xsl:value-of select="format-number($pi * 100,'#.####')"/>The result is 314,1593.
Use format-number to truncate instead of rounding: request one extra formatting position and remove the last character:
<xsl:variable name="pi-to-5-sig" select="format-number($pi,'#.#####')"/>
<xsl:value-of select="substring($pi-to-5-sig,1,string-length($pi-to-5-sig) −1)"/>The result is 3,1415.
XSLT 2.0
The new XPath 2.0 function round-half-to-even( ) handles most cases. The half-to-even rule means that an exact midpoint rounds to the value producing an even result. Thus round-half-to-even(1.115,2) gives 1.12, and round-half-to-even(1.125,2) also gives 1.12! The first rounds up because 2 is even; the second rounds down because three is odd. Across many numbers, rounding up and down should balance rounding errors. The second argument specifies the decimal places.
If a final 5 must always round upward, use the XSLT 1.0 techniques.
Discussion
Multiplying, rounding and dividing works within IEEE floating-point limits. Too many decimal places cause IEEE rules to conflict with the expected result. Asking for 16 decimals of pi, for example, gives only 15:
<xsl:value-of select="round($pi * 10000000000000000) div 10000000000000000"/>The result is 3,141592653589793 rather than 3,1415926535897932.
Another technique manipulates the number as a string and truncates it:
<xsl:value-of select="concat(substring-before($pi,'.'), '.', substring(substring-after($pi,'.'),1,4))"/>
The result is 3,1415.
The effect of ceiling or round can be achieved this way with more complexity:
<xsl:variable name="whole" select="substring-before($pi,'.')"/>
<xsl:variable name="frac" select="substring-after($pi,'.')"/>
<xsl:value-of select="concat($whole, '.', substring($frac,1,3), round(substring($frac,4,2) div 10))"/>The result is 3,1416.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de