[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: XPath / XSLT 2.1 - Rounding algorithms
At 2008-11-13 16:57 -0500, Rushforth, Peter wrote:
A couple of years ago, I ran into a need to call extension functions Not sure where you read that ... the round() function goes to the nearest whole number or does a ceiling() for the value "x.5": http://www.w3.org/TR/2007/REC-xpath-functions-20070123/#func-round Specifically, round-half-up(decimal_num,num_digits), where 'up' means towards positive or negative infinity, is what I'm looking for. Except for the number of digits you could do: if( x < 0 ) then -( round(abs(x)) ) else round(x) ... and use format-number() to handle the digits bit. I hope the example below helps. . . . . . . . . . Ken T:\ftemp>type peter.xsl <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:c="urn:X-Crane" exclude-result-prefixes="xsd c" version="2.0"> <xsl:output method="text"/> <xsl:template match="/"> -2.5: <xsl:value-of select="c:PeterRound(-2.5)"/> 2.5: <xsl:value-of select="c:PeterRound(2.5)"/> </xsl:template> <xsl:function name="c:PeterRound"> <xsl:param name="x"/> <xsl:sequence select="if( $x < 0 ) then -( round(abs($x)) ) else round($x)"/> </xsl:function> </xsl:stylesheet> T:\ftemp>xslt2 peter.xsl peter.xsl con -2.5: -3 2.5: 3 T:\ftemp> -- Upcoming XSLT/XSL-FO hands-on courses: Wellington, NZ 2009-01 Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video sample lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg Video course overview: http://www.youtube.com/watch?v=VTiodiij6gE G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|