Problem encounter while doing calculation

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem encounter while doing calculation

quinquin2209
Hi,

I have an xml instance of a purchase order and want to perform calculation like the following:

SubTotal = Quantity * UnitPrice
Total Amount = sum of subtotal of all line items

I have prepare a binding on LineItem/SubTotal and TotalAmount As follow:


<xforms:bind nodeset="OrderLineItem/SubTotal" calculate="../Quantity * ../UnitPrice"/> 
<xforms:bind nodeset="TotalAmount" calculate="sum(instance('doc-instance')/LineItem/SubTotal)"/> 


In the xform, when i update the quantity and unitprice with some specific combination, unexpected result is calculate.

For example, if Quantity = 200 and Unit Price = 10.524, the expected result is 2104.8, but the actual result is 2104.7999999999997

I am wondering how can I solve the problem.

My testing file is here test.jsp

Thanks in advance

Queenie
Reply | Threaded
Open this post in threaded view
|

Re: Problem encounter while doing calculation

Erik Bruchez
Administrator
By default, XPath numbers are floating-point values. When using  
currency amounts, that's the last thing you want since, as you  
noticed, it can lead to funny rounding errors.

What you do want to use are decimal types. For that to happen, you  
have to explicitly use the xs:decimal() constructor:

   xs:decimal(../Quantity) * xs:decimal(../UnitPrice)
   sum(for $subtotal in instance('doc-instance')/LineItem/SubTotal  
return xs:decimal($subtotal))"

I am not sure how this example you are showing, which in fact has been  
used for a long time by XForms people, is meant to work correctly with  
currencies!

In the future, just assigning an xs:decimal type through a type model  
item property or a schema should be enough, for for now you do have to  
use xs:decimal().

-Erik

On Mar 26, 2008, at 12:55 AM, quinquin2209 wrote:

>
> Hi,
>
> I have an xml instance of a purchase order and want to perform  
> calculation
> like the following:
>
> SubTotal = Quantity * UnitPrice
> Total Amount = sum of subtotal of all line items
>
> I have prepare a binding on LineItem/SubTotal and TotalAmount As  
> follow:
>
>
> <xforms:bind nodeset="OrderLineItem/SubTotal" calculate="../Quantity *
> ../UnitPrice"/>
> <xforms:bind nodeset="TotalAmount"
> calculate="sum(instance('doc-instance')/LineItem/SubTotal)"/>
>
>
> In the xform, when i update the quantity and unitprice with some  
> specific
> combination, unexpected result is calculate.
>
> For example, if Quantity = 200 and Unit Price = 10.524, the expected  
> result
> is 2104.8, but the actual result is 2104.7999999999997
>
> I am wondering how can I solve the problem.
>
> My testing file is here  http://www.nabble.com/file/p16297084/test.jsp
> test.jsp
>
> Thanks in advance
>
> Queenie
> --
> View this message in context: http://www.nabble.com/Problem-encounter-while-doing-calculation-tp16297084p16297084.html
> Sent from the ObjectWeb OPS - Users mailing list archive at  
> Nabble.com.
>
>
> --
> You receive this message as a subscriber of the [hidden email]  
> mailing list.
> To unsubscribe: mailto:[hidden email]
> For general help: mailto:[hidden email]?subject=help
> OW2 mailing lists service home page: http://www.ow2.org/wws
--
Orbeon Forms - Web Forms for the Enterprise Done the Right Way
http://www.orbeon.com/



--
You receive this message as a subscriber of the [hidden email] mailing list.
To unsubscribe: mailto:[hidden email]
For general help: mailto:[hidden email]?subject=help
OW2 mailing lists service home page: http://www.ow2.org/wws