I have an XSLT as below:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="Test">
<Result>
<xsl:value-of select="number(depth)*number(width)*number(height)"/>
</Result>
</xsl:template>
When I test this XSLT against the below sample file in Altova XML or in W3CSchool here, I get the result as 0.128
Sample file:
<?xml version="1.0" encoding="UTF-8"?>
<Test>
<depth>.8</depth>
<width>.8</width>
<height>.2</height>
</Test>
However, things change when I use Java to invoke the XSLT. I get the result as
<Result>0.12800000000000003</Result>
Below is the simple code I'm using:
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
public class TestMain {
public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(new File("transform.xslt"));
Transformer transformer = factory.newTransformer(xslt);
Source text = new StreamSource(new File("input.xml"));
transformer.transform(text, new StreamResult(new File("output.xml")));
}
}
Question: Why is the Java code giving the output as 0.12800000000000003? Even 0.12800000000000000 is understandable, but 0.12800000000000003 is incorrect calculation.