I have noticed some strange precision behaviour that I cannot understand, I have some XML:
<CLD>
<UCRV>
<UCR1>.07</UCR1>
</UCRV>
</CLD>
And in an XSLT file, I am selecting the value as pence (or 100 pence as it seems, I don’t know why but it’s what the customer wants!) as:
<xsl:value-of select="./s0:CLD/s0:UCRV/s0:UCR1/text() * 100 * 100"/>
But this is output as 700.00000000000011 The data type is xsd:Decimal. Is there some default precisioning going on here? I can simply round the number but I just wanted to understand it a bit better.
Thanks
Floating point numbers can’t represent everything precisely. Since the numbers are stored in binary form, numbers that seem to be easy to write in decimal can be actually only approached in binary sometimes. This is the case with 0.07, it’s stored internally as 0.070000000000000011 as it seems in your case. As a rule of thumb, you should never trust floating point values so as to compare them directly without rounding.