任务舍入数量和准确性 XSLT 1.0

使用时我的不一致 xsl,

这 xml,


<rate>
<totalrate>506.41</totalrate>
<totaltax>17</totaltax>
<currency>INR</currency>
</rate>


和 xsl,


xml version="1.0" encoding="UTF-8"?
<xsl:stylesheet version="1.0" xmlns:fo="[url=http://www.w3.org/1999/XSL/Format"]http://www.w3.org/1999/XSL/Format"[/url] xmlns:xsl="[url=http://www.w3.org/1999/XSL/Transform">]http://www.w3.org/1999/XSL/Transform">[/url]
<xsl:output encoding="UTF-8" indent="yes" method="xml" version="1.0"></xsl:output>
<xsl:template match="/">
<totalamount>
<xsl:value-of select="Rate/TotalRate + Rate/TotalTax"></xsl:value-of>
</totalamount>
</xsl:template>
</xsl:stylesheet>


有一条出路,


<totalamount xmlns:fo="[url=http://www.w3.org/1999/XSL/Format">523.4100000000001</totalamount>]http://www.w3.org/1999/XSL/For ... gt%3B[/url]


但它是预期的/P,


<totalamount xmlns:fo="[url=http://www.w3.org/1999/XSL/Format">523.41</totalamount>]http://www.w3.org/1999/XSL/For ... gt%3B[/url]


为什么 o/p - 这是 523.4100000000001? 我怎样才能得到 523.41, 不算吗?
已邀请:

詹大官人

赞同来自:

在 XSLT 1.0 数字用类型实现

double

, 并且,与任何二进制类型的浮动逗号一样,发生精度损耗。

在 XSLT 2.0/XPath 2.0 您可以使用类型
xs:decimal

在没有准确性的情况下工作

.

I. XSLT 1.0 决定

:

使用

功能
format-number//

:


<xsl:stylesheet version="1.0" xmlns:xsl="[url=http://www.w3.org/1999/XSL/Transform">]http://www.w3.org/1999/XSL/Transform">[/url]
<xsl:output indent="yes" omit-xml-declaration="yes"></xsl:output>
<xsl:template match="/*">
<totalamount>
<xsl:value-of select="format-number/TotalRate + TotalTax, '0.##'/"></xsl:value-of>
</totalamount>
</xsl:template>
</xsl:stylesheet>


当此转换适用于提供的文件时

XML:


<rate>
<totalrate>506.41</totalrate>
<totaltax>17</totaltax>
<currency>INR</currency>
</rate>


想要产生的正确结果

:


<totalamount>523.41</totalamount>


这里还有一个示例,显示所需的精度不能静态已知并且可以作为外部传输 / 全局参数

:


<xsl:stylesheet version="1.0" xmlns:xsl="[url=http://www.w3.org/1999/XSL/Transform">]http://www.w3.org/1999/XSL/Transform">[/url]
<xsl:output indent="yes" omit-xml-declaration="yes"></xsl:output>
<xsl:param name="pPrec" select="2"></xsl:param>
<xsl:param name="pPrec2" select="13"></xsl:param>
<xsl:variable name="vPict" select="'##################'"></xsl:variable>
<xsl:template match="/*">
<totalamount>
<xsl:value-of select="format-number/TotalRate + TotalTax,
concat/'0.', substring/$vPict,1,$pPrec//
/"></xsl:value-of>
</totalamount>
<totalamount>
<xsl:value-of select="format-number/TotalRate + TotalTax,
concat/'0.', substring/$vPict,1,$pPrec2//
/"></xsl:value-of>
</totalamount>
</xsl:template>
</xsl:stylesheet>


当此转换适用于提供的文件时 XML, 获得了两种结果 - 精确度 2 和准确性 13

:


<totalamount>523.41</totalamount>
<totalamount>523.4100000000001</totalamount>


II. XSLT 2.0 解决方案使用
xs:decimal


:


<xsl:stylesheet exclude-result-prefixes="xs" version="2.0" xmlns:xs="[url=http://www.w3.org/2001/XMLSchema"]http://www.w3.org/2001/XMLSchema"[/url] xmlns:xsl="[url=http://www.w3.org/1999/XSL/Transform">]http://www.w3.org/1999/XSL/Transform">[/url]
<xsl:output indent="yes" omit-xml-declaration="yes"></xsl:output>
<xsl:strip-space elements="*"></xsl:strip-space>
<xsl:template match="/*">
<totalamount>
<xsl:value-of select="xs:decimal/TotalRate/ + xs:decimal/TotalTax/"></xsl:value-of>
</totalamount>
</xsl:template>
</xsl:stylesheet>


当此转换适用于同一文档时 XML /更高/, 事实证明了所需的,结果正确

:


<totalamount>523.41</totalamount>

要回复问题请先登录注册