Tuesday, May 31, 2011

converting scientific number to Number - XSLT

To conver BigDecimal number in to the normal number in xslt ,we need to write  a new template .
then call this template in the following way
xsl:call-template name="convertSciToNumString"><xsl:with-param name="inputVal" select="percentAmmo" /></xsl:call-template>Template is
<xsl:template name="convertSciToNumString">
<xsl:param name="inputVal" select="0" />
<xsl:variable name="max-exp">
<xsl:value-of
select="'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'" />
</xsl:variable>
<xsl:variable name="numInput">
<xsl:value-of select="translate(string($inputVal),'e','E')" />
</xsl:variable>
<!-- ==== Mantisa ==== -->
<xsl:variable name="numMantisa">
<xsl:value-of select="number(substring-before($numInput,'E'))" />
</xsl:variable>
<!-- ==== Exponent ==== -->
<xsl:variable name="numExponent">
<xsl:choose>
<xsl:when test="contains($numInput,'E+')">
<xsl:value-of select="substring-after($numInput,'E+')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-after($numInput,'E')" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- ==== Coefficient ==== -->
<xsl:variable name="numCoefficient">
<xsl:choose>
<xsl:when test="0 > $numExponent">
<xsl:text>0.</xsl:text>
<xsl:value-of select="substring($max-exp, 1, -number($numExponent)-1)" />
<xsl:text>1</xsl:text>
</xsl:when>
<xsl:when test="$numExponent > 0">
<xsl:text>1</xsl:text>
<xsl:value-of select="substring($max-exp, 1, number($numExponent))" />
</xsl:when>
<xsl:otherwise>
1
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="result">
<xsl:value-of select="number($numCoefficient) * number($numMantisa)" />
</xsl:variable>
<xsl:choose>
<xsl:when test="$result = 'NaN'">
<xsl:value-of select="$numInput" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$result" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<

0 comments:

Post a Comment