xslt 中的十进制精度 [英] Decimal precision in xslt

查看:30
本文介绍了xslt 中的十进制精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XSLT 文件来舍入 XML 中的某些值.因此,这些值中的一个将在十进制精度级别 18(最多 18 位十进制值)四舍五入.我尝试使用 Altova XMLSpy 和 Visual Studio 执行此操作.在这两种情况下,值都在一定数量的数字后被修剪掉.下面是我的代码:

I am using a XSLT file to round off certain values in XML. So of these values are to be rounded of at a decimal precision level 18(upto 18th decimal value). I tried doing this with Altova XMLSpy and Visual Studio. In both the cases the values were trimmed off after certain number of digits. Below is my code:

XML

<DateReceived>20160509</DateReceived>
<CurrentCommitment>0</CurrentCommitment>
<InvestedCapital>1510650</InvestedCapital>
<InsideExpenses>1.4556888888888888888495783095462397857346874356023496</InsideExpenses>
<IEMF>2345.76454</IEMF>

XSLT

<xsl:template match="ROW/InsideExpenses">
    <InsideExpenses>
        <xsl:value-of select="format-number(round(1000000000000000000 * text()) div 1000000000000000000, '#.##################')"/>
    </InsideExpenses>

在 Altova XML 间谍的情况下,标记 InsideExpenses 中的值为 1.4556888888888888,而在 Visual Studio 的情况下,它是 1.45568888888889(小数位后少 1,最后是 9).知道为什么会发生这种情况以及如何将数字四舍五入到小数点后 18 位会很有帮助.

In case of Altova XML spy the value in tag InsideExpenses was 1.4556888888888888 and in case of Visual studio it was 1.45568888888889(1 less decimal space and 9 in the end). It would be great help to know why this is happening and how can I round the number to 18th decimal place.

推荐答案

您首先需要在这里清楚 XSLT 2.0 和 1.0 之间的区别.XSLT 2.0(在 Altova 中实现,但这并不意味着您正在使用它)支持 xs:double 和 xs:decimal 数字数据类型.XSLT 1.0 仅支持双精度浮点 (xs:double).

You first need to be clear about the differences between XSLT 2.0 and 1.0 here. XSLT 2.0 (which is implemented in Altova, but that doesn't mean you are using it) supports both xs:double and xs:decimal numeric data types. XSLT 1.0 only supports double-precision floating point (xs:double).

对于浮点数,如果结果不能精确地用十进制表示,或者如果精度超过大约 15 位,算术将始终产生舍入错误.使用 xs:decimal,实现可能支持无限精度,在这种情况下小数结果将是精确的,或者它可能支持固定精度并因此进行四舍五入.

With floating point, arithmetic will always produce rounding errors if the result isn't representable exactly in decimal, or if there are more than about 15 digits of precision. With xs:decimal, the implementation may either support infinite precision, in which case decimal results will be exact, or it may support a fixed precision and therefore do rounding.

因此,如果您想要 18 位精度的数字运算,您确实需要使用 XSLT 2.0 和 xs:decimal 数据类型.

So if you want 18 digits of precision for numeric operations, you really need to be using XSLT 2.0 and the xs:decimal data type.

这篇关于xslt 中的十进制精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆