在 xsl 中格式化科学数字表示 [英] Formatting scientific number representation in xsl
问题描述
我的 XML -1.8959581529998104E-4 中有以下值.我想将其格式化为应该使用 XSL 给我 -0.000189595815299981 的确切数字.
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') 给我 NaN.
format-number(-1.8959581529998104E-4,'0.000000;-0.000000') gives me NaN.
有什么想法吗?
干杯
安德斯
推荐答案
XSLT 1.0 不支持科学记数法.
这个:number('-1.8959581529998104E-4')
结果:NaN
这个:number('-0.000189595815299981')
结果:-0.000189595815299981
XSLT 2.0 支持科学记数法
XSLT 2.0 has support for scientific notation
这个:number('-1.8959581529998104E-4')
结果:-0.000189595815299981
编辑:一个非常简单的 XSLT 1.0 解决方法:
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>
使用此输入:
<number>-1.8959581529998104E-4</number>
输出:
-0.00018959581529998104
这篇关于在 xsl 中格式化科学数字表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!