Formatting scientific number representation in xsl

format, numbers, xslt

Solution

XSLT 1.0 does not have 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

Problem

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?

Original source