Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
620 views
in Technique[技术] by (71.8m points)

c# - Decimal precision in xslt

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>

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...