在xsl中格式化科学数字表示 [英] Formatting scientific number representation in xsl
问题描述
在我的XML -1.8959581529998104E-4中有以下值。我想格式化到它应该使用XSL的确切数字给我-0.000189595815299981。格式数字(-1.8959581529998104E-4,'0.000000; -0.000000')给了我NaN。
任何想法?
干杯
安德兹
XSLT 1.0不支持科学记数法。 $ b
结果: NaN
<
number(' - 0.000189595815299981')
结果:
-0.000189595815299981
XSLT 2.0支持科学记数法
$ b
这个: number(' - 1.8959581529998104E-4')
结果: -0.000189595815299981
编辑:一个非常简单的XSLT 1.0解决方法:
< xsl:stylesheet version =1.0xmlns:xsl =http: //www.w3.org/1999/XSL/Transform\">
select =substring('100000000000000000000000000000000000000000000',
1,substring($ vExponent,2)+ 1)/>
< xsl:when test =starts-with($ vExponent,' - ')>
< xsl:value-of select =$ vMantissa div $ vFactor/>
< / xsl:when>
< xsl:otherwise>
< xsl:value-of select =$ vMantissa * $ vFactor/>
< / xsl:否则>
< / xsl:template>
< / xsl:stylesheet>
有了这个输入:
pre > <数> -1.8959581529998104E-4; /数>
输出:
-0.00018959581529998104
I have the following value in my XML -1.8959581529998104E-4. I want to format this to the exact number it should be using XSL to give me -0.000189595815299981.
format-number(-1.8959581529998104E-4,'0.000000;-0.000000') gives me NaN.
Any ideas?
Cheers
Andez
XSLT 1.0 has not support for scientific notation.
This: number('-1.8959581529998104E-4')
Result: NaN
This: number('-0.000189595815299981')
Result: -0.000189595815299981
XSLT 2.0 has support for scientific notation
This: number('-1.8959581529998104E-4')
Result: -0.000189595815299981
EDIT: A very simple XSLT 1.0 workaround:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="number[substring-after(.,'E')]">
<xsl:variable name="vExponent" select="substring-after(.,'E')"/>
<xsl:variable name="vMantissa" select="substring-before(.,'E')"/>
<xsl:variable name="vFactor"
select="substring('100000000000000000000000000000000000000000000',
1, substring($vExponent,2) + 1)"/>
<xsl:choose>
<xsl:when test="starts-with($vExponent,'-')">
<xsl:value-of select="$vMantissa div $vFactor"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$vMantissa * $vFactor"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
With this input:
<number>-1.8959581529998104E-4</number>
Output:
-0.00018959581529998104
这篇关于在xsl中格式化科学数字表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!