Project

Profile

Help

string to number conversion in addition

Added by Anonymous almost 19 years ago

Legacy ID: #3489022 Legacy Poster: Jens Springer (jenger)

Hi, i have a problem with some additions and saxon 8.6: <xsl:value-of select="'13.4' cast as xs:double + '13.8' cast as xs:double"/> <xsl:value-of select="13.4 + '13.8' cast as xs:double"/> <xsl:value-of select="number('13.4') + number('13.8')"/> <xsl:value-of select="number('13.4') + 13.8"/> In all cases i get 27.200000000000003 as result. Also, the addition "0.4" + "5.2" results 5.6000000000000005. Thank you, jenger


Replies (1)

RE: string to number conversion in addition - Added by Anonymous almost 19 years ago

Legacy ID: #3489044 Legacy Poster: Michael Kay (mhkay)

I think these results are almost certainly correct according to the specification: you will get similar results from other languages such as Java. The rounding errors occur because decimal numbers such as 13.4 cannot be represented precisely by any binary floating point number, so they are held as an approximation. XSLT 2.0 offers the xs:decimal data type so that you can do precise arithmetic in decimal. Michael Kay

    (1-1/1)

    Please register to reply