Ganesh K
Ganesh K

Reputation: 1

Multiplying float number in XSLT

I want to multiply float number in a node with 1000 and the result I have to map to target node. Here is the XSLT code I have tried.

mnLineNumber * 1000

Here mnLineNumber node is float data type and it has value 1.0005. I have expected the result as 1005. But, I am getting the result as

<ns0:szTarget>1004.9999999999999</ns0:szTarget>.

Then, I have used the round function and I got the expected result.

round(ns0:mnLineNumber * 1000.0).

But, my question is why I am getting a floating number if I am not using round function in xslt?

Upvotes: 0

Views: 875

Answers (1)

Michael Kay
Michael Kay

Reputation: 163458

You say that mnLineNumber node is float data type and it has value 1.0005. But it doesn't. There is no float (or double) number with that value, because in general fractional decimal values cannot be represented precisely in binary. (Just as the fraction one-third cannot be represented precisely in decimal.) So when you write the string 1.0005 as the lexical representation of an xs:double, the xs:double number you get actually has a value something like 1.00049999999999999. When you multiply the number by 1000, the difference between its actual value and the "intended" value becomes magnified.

In XSLT 2.0 you can get round this by using the xs:decimal data type in place of xs:double. In XSLT 1.0, just round the answer as required.

Upvotes: 0

Related Questions