AW: [xsl] round-half-to-even problem.

Subject: AW: [xsl] round-half-to-even problem.
From: "Szabo, Patrick \(LNG-VIE\)" <patrick.szabo@xxxxxxxxxxxxx>
Date: Mon, 24 Jan 2011 16:01:06 +0100
Thx for your fast response.
The problem is that I'm writing a transformation that turns an Excel sheet
into xml.
Unfortunately the excel is filled with formulas.
In the output xml of Office 2007 the number is not 298,16 (which stands in the
excel sheet) but its 298.15499999999997.

So I have to find a way to round exactly like Excel does, because the customer
cares about 1 Cent.

regards


. . . . . . . . . . . . . . . . . . . . . . . . . .
Patrick Szabo
 XSLT-Entwickler
LexisNexis
Marxergasse 25, 1030 Wien

mailto:patrick.szabo@xxxxxxxxxxxxx
Tel.: +43 (1) 534 52 - 1573
Fax: +43 (1) 534 52 - 146


-----UrsprC<ngliche Nachricht-----

Von: G. Ken Holman [mailto:gkholman@xxxxxxxxxxxxxxxxxxxx]
Gesendet: Montag, 24. JC$nner 2011 15:49
An: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Betreff: Re: [xsl] round-half-to-even problem.

At 2011-01-24 15:39 +0100, Szabo, Patrick \(LNG-VIE\) wrote:
>I'm using saxon 9 EE and xslt 2.0.
>I want to round but I'm getting weird outputs.

Because you aren't "rounding to the closest digit", you are "rounding
to the closest even parity digit".

>My Template looks like this:
>
><xsl:template match="data">
>                 <absatz>
>                         <xsl:choose>
>                                 <xsl:when test="@type='Number' and
>string-length(substring-after(., '.')) &gt; 2">
>                                         <xsl:value-of
>select="round-half-to-even(text(), 2)"/>
>                                 </xsl:when>
>                                 <xsl:otherwise>
>                                         <xsl:apply-templates/>
>                                 </xsl:otherwise>
>                         </xsl:choose>
>                 </absatz>
></xsl:template>
>
>
>Example:
><data type="Number">1351.845</data>
>
>Output:
><absatz>1351.84</absatz>

Right ... because the "5" is half-rounding the "4" to "4".  "4" is
the closest even-parity digit to the value "4.5".  "6" is the closes
even-parity digit to the value "5.5".

>Desired:
><absatz>1351.85</absatz>
>
>Example:
><data type="Number">298.15499999999997</data>
>
>Output:
><absatz>298.16</absatz> <- which is correct or at least what I want.

That surprises me ... I would have expected "298.15" because the next
digit is not simply "5" or above.  I'm guessing it has to do with the
binary representation of the number.

>Can anybody explain this to why on time it takes the bigger and the next
>time the smaller number ?!

Because "round-half-to-even" rounds to an even-parity digit
(0,2,4,6,8) as you request.

>Can I control that somehow ?!

If you are rounding for presentation purposes, then use:

   format-string(.,'.00')

... which rounds the third digit to the second digit simply, where
"5" goes to the ceiling (a negative number gets smaller, a positive
number gets bigger).

I hope this helps.

. . . . . . . . . Ken

--
Contact us for world-wide XML consulting & instructor-led training
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread