我需要根據指定的小數位數進行格式化。使用指定的小數位數格式化雙精度值
如:價值= 0.012877 小數位數= 3
format-number(value,'how to declare here based on specified decimal places')
我知道一般格式爲:號格式(價值,「0.00」),但這裏的小數位數是固定的,但在我的情況下,它會根據變更我的小數位,所以請任何人幫助我。
謝謝, @nag
我需要根據指定的小數位數進行格式化。使用指定的小數位數格式化雙精度值
如:價值= 0.012877 小數位數= 3
format-number(value,'how to declare here based on specified decimal places')
我知道一般格式爲:號格式(價值,「0.00」),但這裏的小數位數是固定的,但在我的情況下,它會根據變更我的小數位,所以請任何人幫助我。
謝謝, @nag
以下序列構造...
<xsl:variable name="decimalPlaces" select="3" />
<xsl:value-of select="format-number(0.012877, substring('0.0000000000000',1,$decimalPlaces+2))"/>
... ... EMIT
3210...爲字符串。
在指令的值中,寫入與$ decimalPlaces的最大範圍一樣多的零。
下面是一個簡單XSLT 1.0溶液不需要零的(潛在無限)字符串:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="pNum" select="0.012877"/>
<xsl:param name="pPrec" select="3"/>
<xsl:template match="/">
<xsl:value-of select="substring-before(concat($pNum, '.'), '.')"/>
<xsl:if test="contains($pNum, '.')">
<xsl:text>.</xsl:text>
<xsl:value-of select="substring(substring-after($pNum,'.'), 1, $pPrec)"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
當此轉換適用於任何XML文檔(未使用),所需的正確結果將生成:
0.012
謝謝你的工作。除了採用最大零來格式化之外,還有其他方法可以做到嗎? – nag
您使用XSLT 1.0嗎?或2.0? –
我使用的是1.0,它可以告訴我在2.0中還有什麼辦法可以做。 – nag