乘法后保留尾部的零,XSLT。

问题描述 投票:0回答:1

我有一个问题,我需要在数学计算后保留尾部的0,例如:9854.32000*1应该返回9854.32000而不是9854.32。

例如:9854.32000 * 1 应该返回 9854.32000 而不是 9854.32。我尝试使用

<xsl:decimal-format name="test" decimal-separator="."/>

<xsl:value-of select="format-number(26825.8000 * 1, '#.000', 'test')"/>

但我想知道是否有一种方法可以让我通过计算尾部零的长度,并将它们追加到结果中,从而使我可以通用地做到这一点。

请教

xml xslt xhtml xslt-2.0
1个回答
1
投票

画面 一串 format-number() 可以计算。考虑下面的例子。

XML

<input>
    <multiplicand>1</multiplicand>
    <multiplicand>2.0</multiplicand>
    <multiplicand>3.14</multiplicand>
    <multiplicand>4.000</multiplicand>
    <multiplicand>5.0000</multiplicand>
    <multiplicand>6.12345</multiplicand>
    <multiplicand>7.000000</multiplicand>
</input>

XSLT

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:param name="multiplier" select="2"/>

<xsl:template match="/input">
    <output>
        <xsl:for-each select="multiplicand">
            <xsl:variable name="zeros" select="translate(substring-after(., '.'), '123456789', '000000000')" />
            <product>
                <xsl:value-of select="format-number(. * $multiplier, concat('#.', $zeros))" />
            </product>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <product>2</product>
   <product>4.0</product>
   <product>6.28</product>
   <product>8.000</product>
   <product>10.0000</product>
   <product>12.24690</product>
   <product>14.000000</product>
</output>
© www.soinside.com 2019 - 2024. All rights reserved.