XSLT 2.0:计算结果四舍五入到两位小数

Siw*_*org 2 xslt rounding-error rounding xslt-2.0 xpath-2.0

我正在使用 xslt 2.0 和 Saxon 9.6,需要将价格金额和数量相乘,然后将结果四舍五入到小数点后两位。我不想要银行四舍五入,我想要四舍五入所以 .495 = 0.50 和 .494 = .49 。我尝试阅读有关此问题的几篇文章和帖子,但找不到解决方案。我看到几个地方由于浮点问题而使用 xslt 1.0 进行舍入是有问题的,并且很多人提到 xslt 2.0 和 xs:decimal 应该可以解决问题,但我似乎找不到“防水”解决方案。

我有一个 xml 文件(发票),其中包含 4 个不同的 InvoiceLines,其中包含价格和数量元素:

<Invoice>
<ID>12345</ID>
<IssueDate>2012-11-21</IssueDate>

<Supplier>
    <Party>
        <ID>977187761</ID>
    </Party>
</Supplier>
<Customer>
    <Party>
        <ID schemeID="NO:ORGNR">810305282</ID>
    </Party>
</Customer>
<Delivery>
    <DeliveryDate>2012-11-21</DeliveryDate>
</Delivery>
<TaxTotal>
    <TaxAmount currencyID="NOK">128.89</TaxAmount>
</TaxTotal>
<InvoiceLine>
    <ID>1</ID>
    <Quantity unitCode="EA">19</Quantity>
    <LineAmount currencyID="NOK">130.26</LineAmount>
    <Item>
        <Name>TestItem</Name>
    </Item>
    <Price currencyID="NOK">8.569736842105263</Price>
</InvoiceLine>
<InvoiceLine>
    <ID>2</ID>
    <Quantity unitCode="NAR">1.00</Quantity>
    <LineAmount currencyID="NOK">128.2</LineAmount>
    <Item>
        <Name>Vare A</Name>
    </Item>
    <Price currencyID="NOK">128.195</Price>
</InvoiceLine>
<InvoiceLine>
    <ID>3</ID>
    <Quantity unitCode="NAR">1.00</Quantity>
    <LineAmount currencyID="NOK">128.7</LineAmount>
    <Item>
        <Name>Vare B</Name>
    </Item>
    <Price currencyID="NOK">128.695</Price>
</InvoiceLine>
<InvoiceLine>
    <ID>4</ID>
    <Quantity unitCode="NAR">1.00</Quantity>
    <LineAmount currencyID="NOK">128.4</LineAmount>
    <Item>
        <Name>Vare C</Name>
    </Item>
    <Price currencyID="NOK">128.395</Price>
</InvoiceLine>
Run Code Online (Sandbox Code Playgroud)

我在 xml 上运行了以下 xslt:

<xsl:template match="/">
    <html>
        <body>
            <xsl:for-each select="//InvoiceLine">
                <tr>

                    <td>Line: <xsl:value-of select="ID"/>  </td>

                    <td>Price: <xsl:value-of select="Price"/></td>

                    <td>Quantity: <xsl:value-of select="Quantity"/></td>

                    <td>LineAmount: <xsl:value-of select="LineAmount"/></td>

                    <td>P*Q: <xsl:value-of select="Price * Quantity"/></td>

                    <td>Round(p*q*100)div 100: <xsl:value-of select="round((Price * Quantity * 100)) div 100" /></td>

                    <td>xs:decimal(p*q): <xsl:value-of select="xs:decimal(Price * Quantity)" /></td>

                    <td>round((p*q)*10*10) div 100: <xsl:value-of select="round((Price * Quantity) * 10 * 10) div 100"/></td>


                </tr>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

这是结果:

<html xmlns:saxon="http://saxon.sf.net/" xmlns:op="http://www.w3.org/2002/08/xquery-operators" xmlns:schold="http://www.ascc.net/xml/schematron" xmlns:iso="http://purl.oclc.org/dsdl/schematron" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ubl="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
   <body>
  <tr>
     <td>Line: 1</td>
     <td>Price: 8.569736842105263</td>
     <td>Quantity: 19</td>
     <td>LineAmount: 130.26</td>
     <td>P*Q: 162.825</td>
     <td>Round(p*q*100)div 100: 162.82</td>
     <td>xs:decimal(p*q): 162.82499999999998863131622783839702606201171875</td>
     <td>round((p*q)*10*10) div 100: 162.83</td>
  </tr>
  <tr>
     <td>Line: 2</td>
     <td>Price: 128.195</td>
     <td>Quantity: 1.00</td>
     <td>LineAmount: 128.2</td>
     <td>P*Q: 128.195</td>
     <td>Round(p*q*100)div 100: 128.2</td>
     <td>xs:decimal(p*q): 128.19499999999999317878973670303821563720703125</td>
     <td>round((p*q)*10*10) div 100: 128.19</td>
  </tr>
  <tr>
     <td>Line: 3</td>
     <td>Price: 128.695</td>
     <td>Quantity: 1.00</td>
     <td>LineAmount: 128.7</td>
     <td>P*Q: 128.695</td>
     <td>Round(p*q*100)div 100: 128.7</td>
     <td>xs:decimal(p*q): 128.69499999999999317878973670303821563720703125</td>
     <td>round((p*q)*10*10) div 100: 128.69</td>
  </tr>
  <tr>
     <td>Line: 4</td>
     <td>Price: 128.395</td>
     <td>Quantity: 1.00</td>
     <td>LineAmount: 128.4</td>
     <td>P*Q: 128.395</td>
     <td>Round(p*q*100)div 100: 128.4</td>
     <td>xs:decimal(p*q): 128.395000000000010231815394945442676544189453125</td>
     <td>round((p*q)*10*10) div 100: 128.4</td>
  </tr>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我得到了不同类型的计算/舍入的不同结果。对于第 1 行,我在上次计算中得到了预期结果,但对于第 2 行和第 3 行,此计算没有给出预期结果。

如果您提供有关如何解决此问题的任何提示,我将不胜感激。

mic*_*57k 5

怎么样:

<xsl:value-of select="format-number(Price * Quantity, '0.00')"/>
Run Code Online (Sandbox Code Playgroud)

另请注意,我得到了与所有这些相同的预期结果:

<xsl:value-of select="round(Price * Quantity * 100) div 100" />
Run Code Online (Sandbox Code Playgroud)

和:

<xsl:value-of select="round(xs:decimal(Price) * xs:decimal(Quantity) * 100) div 100" />
Run Code Online (Sandbox Code Playgroud)

和:

<xsl:value-of select="round(xs:decimal(Price) * xs:decimal(Quantity) * 10 * 10) div 100"/>
Run Code Online (Sandbox Code Playgroud)

更正:

如果您不想要“银行家四舍五入”,那么您不能使用该format-number()函数,因为它的四舍五入功能与此处相同round-half-to-even()- 请参阅此处的第 5 点: http: //www.w3.org/TR/xslt20/#formatting-号码

但是,所有其他方法都会按预期舍入到最接近的整数。


Mic*_*Kay 5

我假设您没有使用模式感知,这意味着 Price 和 Quantity 等值是非类型原子的。如果使用无类型原子值作为算术运算符的输入,它将被视为双精度浮点。因此,您应该做的第一件事是显式转换为 xs:decimal:您的代码xs:decimal(Price * Quantity)使用双精度算术进行乘法,并将结果转换为十进制,而xs:decimal(Price) * xs:decimal(Quantity)将使用十进制算术。

如果您确保在进行任何算术之前将非类型化值全部转换为十进制,则结果的舍入和格式化应该“按预期”工作。