xslt中的1.16*100产生115.99999999999999

Vex*_*toR 1 xslt xslt-2.0

如果我尝试在xslt 2.0中乘以1.6 * 100它将导致115.99999999999999

如何强迫它结果116

Mar*_*nen 5

您确定version="2.0"在样式表中使用了像Saxon 9这样的XSLT 2.0处理器1.6 * 100吗?XPath表达式包含样本中的数字文字?

因为在这种情况下你应该得到一个精确的结果,例如

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

<xsl:output method="text"/>

<xsl:template name="main">
  <xsl:value-of select="1.16 * 100"/>
</xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

撒克逊人9.4输出116.

结果与version="1.0"例如不同

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

<xsl:output method="text"/>

<xsl:template name="main">
  <xsl:value-of select="1.16 * 100"/>
</xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

我收到警告"运行带有XSLT 2处理器的XSLT 1样式表"和输出115.99999999999999.

因此,使用XSLT 2.0处理器并且version="2.0"在您的代码中您不应该遇到问题,文字代表xs:decimal数字.

如果您处理XML输入,那么它与例如不同

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

<xsl:output method="text"/>

<xsl:template match="item">
  <xsl:value-of select="a * b"/>
</xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

和输入

<root>
  <item>
    <a>1.16</a>
    <b>100</b>
  </item>
</root>
Run Code Online (Sandbox Code Playgroud)

你得到115.99999999999999.

在这种情况下,您应该确保处理器与xs:decimals一起工作

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs"
  version="2.0">

<xsl:output method="text"/>

<xsl:template match="item">
  <xsl:value-of select="xs:decimal(a) * xs:decimal(b)"/>
</xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)