[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: sum() function giving strange result

Subject: RE: sum() function giving strange result
From: "Arun Sinha" <arunsinha666@xxxxxxxxxxx>
Date: Tue, 14 Dec 2004 07:36:38 +0000
xsl sum function
Thanks Jarno.


From: <Jarno.Elovirta@xxxxxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: RE:  sum() function giving strange result
Date: Tue, 14 Dec 2004 09:23:54 +0200

Hi,

> Hi Jarno,
>
> Thanks a lot for the information.
>
> Well, it does work if the values of are of upto 2 decimal places.
>
> But the problem is my stylesheet is a generic one and is
> being applied to
> different XML files. The other XML may contain values upto 3
> or 4 decimal
> places.
>
> So I can't use a value of 100 for multiplication and division.
>
> But your reply definately has given me an idea, how to achieve it.
>
> I am planning to count the digits after decimal and then apply
> multiplication and
> division accordingly.

Count the maximum number of decimal places and then use that as the factor? Sure, that'll probably work. However, in the end you have to just accept that fact that XPath 1.0 uses floating-point numbers and that they can sometimes give you unexpected results.

Cheers,

Jarno

> Any other idea?
>
> Arun
>
>
> >From: <Jarno.Elovirta@xxxxxxxxx>
> >Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> >To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
> >Subject: RE:  sum() function giving strange result
> >Date: Tue, 14 Dec 2004 08:34:45 +0200
> >
> >Hi,
> >
> > > The sum funciton is giving some strange result.
> > >
> > > Xml :-
> > > <documents>
> > >   <value>10.11</value>
> > >   <value>20.22</value>
> > >   <value>30.33</value>
> > >   <value>40.44</value>
> > >   <value>50.55</value>
> > >   <value>10.11</value>
> > >   <value>20.22</value>
> > > </documents>
> > >
> > > XSL :-
> > > <?xml version="1.0" encoding="UTF-8"?>
> > > <xsl:stylesheet version="1.0"
> > > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> > > 	xmlns:exsl="urn:schemas-microsoft-com:xslt"
> > >                 extension-element-prefixes="exsl">
> > >
> > > <xsl:variable name="SubTotals">
> > >   <xsl:for-each  select="/documents/value">
> > >     <value><xsl:value-of select="." /></value>
> > >   </xsl:for-each>
> > > </xsl:variable>
> > >
> > > <xsl:template match="/">
> > >     <xsl:apply-templates select="documents/value" />
> > > </xsl:template>
> > >
> > > <xsl:template match="value">
> > >      <xsl:variable name="Position" select="position()" />
> > >      <xsl:value-of
> > > select="sum(exsl:node-set($SubTotals)/value[position()
> > > &lt; $Position])" /><br />
> > > </xsl:template>
> > >
> > > </xsl:stylesheet>
> > >
> > >
> > > The result :-
> > > 0
> > > 10.11
> > > 30.33
> > > 60.66
> > > 101.1
> > > 151.64999999999998
> > > 161.76
> > >
> > > I am worried about the output 151.64999999999998. Why is it
> > > outputting this
> > > value?
> >
> >Because XPath uses a floating-point numbers
> ><http://www.w3.org/TR/xpath#numbers>.
> >
> > > I am expecting to be upto 2 decimal places or 1 decimal places.
> > >
> > > Well I can mask it upto 2 decimal places but I do not have a
> > > liberty to do
> > > that.
> >
> >How about
> >
> >   <xsl:variable name="SubTotals">
> >     <xsl:for-each  select="/documents/value">
> >       <value>
> >         <xsl:value-of select=". * 100" />
> >       </value>
> >     </xsl:for-each>
> >   </xsl:variable>
> >   ...
> >   <xsl:template match="value">
> >     <xsl:variable name="Position" select="position()" />
> >     <xsl:value-of
> select="sum(exsl:node-set($SubTotals)/value[position()
> >&lt; $Position]) div 100" />
> >     <br />
> >   </xsl:template>
> >
> >Seems to give the expecter result.
> >
> >0
> >10.11
> >30.33
> >60.66
> >101.1
> >151.65
> >161.76
> >
> >Cheers,
> >
> >Jarno
> >
>
> _________________________________________________________________
> Searching for your soulmate? Zero in on the perfect choice.
> http://www.astroyogi.com/newmsn/astrodate/ Try MSN Astrodate now!


_________________________________________________________________
Hey there NRIs! Desi news, films, n more! http://www.msn.co.in/nri/ Stay in the loop!


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.