The logic doesn't appear to work correctly in the above answers by Moop and user357812 when determining vFactor
in one particular scenario.
If vExponent
is a single-digit positive number (without a preceding '+' sign), then vFactor
is set to an empty string. This is because an assumption was made that the 1st character of vExponent
would be a plus/minus sign and therefore the 2nd character onwards were of interest. The vMantissa
variable is then multiplied by an empty string which results in the template outputting NaN
.
If vExponent
is a multi-digit positive number (without a preceding '+' sign), then vFactor
is set to an incorrect value. Because of the aforementioned assumption, the 1st digit is ignored and the vMantissa
is then multiplied by an incorrect vFactor
.
Therefore, I've modified the previously posted code a little so that it can handle scientific numbers of the forms: 2E-4, 2E+4 and 2E4.
<xsl:template name="convertSciToNumString" >
<xsl:param name="inputVal" select="0"/>
<xsl:variable name="vMantissa" select="substring-before(., 'E')"/>
<xsl:variable name="vExponent" select="substring-after(., 'E')"/>
<xsl:variable name="vExponentAbs" select="translate($vExponent, '-', '')"/>
<xsl:variable name="vFactor" select="substring('100000000000000000000000000000000000000000000', 1, substring($vExponentAbs, 1) + 1)"/>
<xsl:choose>
<xsl:when test="number($inputVal)=$inputVal">
<xsl:value-of select="$inputVal"/>
</xsl:when>
<xsl:when test="starts-with($vExponent,'-')">
<xsl:value-of select="format-number($vMantissa div $vFactor, '#0.#############')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number($vMantissa * $vFactor, '#0.#############')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>