xslt中的小数精度

编程入门 行业动态 更新时间:2024-10-22 11:38:18
本文介绍了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中的小数精度

本文发布于:2023-11-23 01:30:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619625.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数   精度   xslt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!