XSLT 2.0:计算结果四舍五入到两位小数
XSLT 2.0: Rounding result of calculation to two decimals
我正在使用 xslt 2.0 和 Saxon 9.6,需要将价格金额和数量相乘,然后将结果四舍五入到两位 decimals.I不想进行银行家四舍五入,我想四舍五入 .495 = 0.50 和 .494 = .49。我已尝试阅读有关此问题的几篇文章和帖子,但找不到解决方案。由于浮点问题,我看到几个地方用 xslt 1.0 进行舍入是有问题的,很多人都提到 xslt 2.0 和 xs:decimal 应该可以解决问题,但我似乎找不到 "waterproof"解决方案。
我有一个 xml 文件(发票),其中包含 4 个不同的发票行,其中包含价格和数量元素:
<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>
我 运行 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>
这是结果:
<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>
如您所见,我得到了不同类型 calculations/rounding 的不同结果。对于第 1 行,我在最后一次计算中得到了预期的结果,但是对于第 2 行和第 3 行,此计算没有给我预期的结果。
如有任何解决此问题的提示,我将不胜感激。
怎么样:
<xsl:value-of select="format-number(Price * Quantity, '0.00')"/>
另请注意,我得到了所有这些相同的预期结果:
<xsl:value-of select="round(Price * Quantity * 100) div 100" />
和:
<xsl:value-of select="round(xs:decimal(Price) * xs:decimal(Quantity) * 100) div 100" />
和:
<xsl:value-of select="round(xs:decimal(Price) * xs:decimal(Quantity) * 10 * 10) div 100"/>
更正:
如果您不想 "banker's rounding",那么您不能使用 format-number()
函数,因为它与 round-half-to-even()
进行相同的舍入 - 请参见此处的第 5 点:http://www.w3.org/TR/xslt20/#formatting-the-number
但是,所有其他方法都会按预期四舍五入到最接近的整数。
我假设您没有使用架构感知,这意味着价格和数量等值是非类型化原子的。如果您使用 untypedAtomic 值作为算术运算符的输入,它将被视为双精度浮点数。因此,您应该做的第一件事是显式转换为 xs:decimal:您的代码 xs:decimal(Price * Quantity)
使用双精度算术进行乘法运算,并将结果转换为小数,而 xs:decimal(Price) * xs:decimal(Quantity)
将使用十进制算术。
如果您确保在进行任何算术运算之前将未类型化的值全部转换为十进制,则结果的四舍五入和格式设置应该有效 "as expected"。
我正在使用 xslt 2.0 和 Saxon 9.6,需要将价格金额和数量相乘,然后将结果四舍五入到两位 decimals.I不想进行银行家四舍五入,我想四舍五入 .495 = 0.50 和 .494 = .49。我已尝试阅读有关此问题的几篇文章和帖子,但找不到解决方案。由于浮点问题,我看到几个地方用 xslt 1.0 进行舍入是有问题的,很多人都提到 xslt 2.0 和 xs:decimal 应该可以解决问题,但我似乎找不到 "waterproof"解决方案。
我有一个 xml 文件(发票),其中包含 4 个不同的发票行,其中包含价格和数量元素:
<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>
我 运行 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>
这是结果:
<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>
如您所见,我得到了不同类型 calculations/rounding 的不同结果。对于第 1 行,我在最后一次计算中得到了预期的结果,但是对于第 2 行和第 3 行,此计算没有给我预期的结果。
如有任何解决此问题的提示,我将不胜感激。
怎么样:
<xsl:value-of select="format-number(Price * Quantity, '0.00')"/>
另请注意,我得到了所有这些相同的预期结果:
<xsl:value-of select="round(Price * Quantity * 100) div 100" />
和:
<xsl:value-of select="round(xs:decimal(Price) * xs:decimal(Quantity) * 100) div 100" />
和:
<xsl:value-of select="round(xs:decimal(Price) * xs:decimal(Quantity) * 10 * 10) div 100"/>
更正:
如果您不想 "banker's rounding",那么您不能使用 format-number()
函数,因为它与 round-half-to-even()
进行相同的舍入 - 请参见此处的第 5 点:http://www.w3.org/TR/xslt20/#formatting-the-number
但是,所有其他方法都会按预期四舍五入到最接近的整数。
我假设您没有使用架构感知,这意味着价格和数量等值是非类型化原子的。如果您使用 untypedAtomic 值作为算术运算符的输入,它将被视为双精度浮点数。因此,您应该做的第一件事是显式转换为 xs:decimal:您的代码 xs:decimal(Price * Quantity)
使用双精度算术进行乘法运算,并将结果转换为小数,而 xs:decimal(Price) * xs:decimal(Quantity)
将使用十进制算术。
如果您确保在进行任何算术运算之前将未类型化的值全部转换为十进制,则结果的四舍五入和格式设置应该有效 "as expected"。