[xsl] Floating point error

Subject: [xsl] Floating point error
From: "Thang X. Le" <tle@xxxxxxxxxxx>
Date: Wed, 25 Jan 2006 09:22:28 -0800
Hi,

I just have a simple problem but can't seem to find the
solution. Given this xml structure:
<product>
   <quantity>3000</quantity>
   <price>6.4</price>
</product>
(the actual quantity and price may vary, but they're always
valid numbers).
I just have to compute the total price, which is:
<xsl:value-of select="format-number(price * quantity,'0.00')"/>

But the floating point number calculation is arbitrarily incorrect.
I may get 19199.91, or 19200. Similarly with other values of
price and quantity. It seems the incorrect result appears at
random.
Even if I try to round off price beforehand, nothing changes:
<xsl:value-of select="format-number(format-number(price, '0.0') * quantity,'0.00')"/>

I'm using dom4j, Java 1.4.1, SunOS. On Windows the calculations
appear to be correct.
So, is it an OS thing? Is there any workaround?
Thanks,

Thang Le

Current Thread