RE: [xsl] Calculate mantissa and exponent?

Subject: RE: [xsl] Calculate mantissa and exponent?
From: "Mitchell, Edmund" <EMitchell@xxxxxxx>
Date: Fri, 10 Aug 2001 06:11:21 -0700
Thanks, Tim.  I did, and it was a small part of what I needed.  If anyone
else needs it, here it is, courtesy of sdussinger:

sample.xml:

<a>
  <num>0.00000004</num>
  <num>123456</num>
  <num>-756444</num>
</a>

sample.xsl:

<xsl:stylesheet 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
      version="1.0">
    <xsl:template match="/">
      <xsl:for-each select='/a/num'>
		<xsl:call-template name="normalize-number">
			<xsl:with-param name="num" select="."/>
    	      </xsl:call-template>
            <xsl:text>&#10;</xsl:text>
      </xsl:for-each>
    </xsl:template>

<!--
  Generic template to normalize a number
  Pass a param called "num" containing the number you wish
  to normalize
-->
<xsl:template name="normalize-number">
 <xsl:param name="num" select="0"/>

 <xsl:call-template name="do-normalize">
   <xsl:with-param name="mantissa" select="$num"/>
   <xsl:with-param name="exp" select="0"/>
 </xsl:call-template>

</xsl:template>

<!--
  Simple template for formatting the exponent. This
  is needed to ensure that the '+' sign shows up for
  positive exponents.
-->
<xsl:template name="format-exp">
 <xsl:param name="exp" select="0"/>

 <xsl:if test="$exp &gt;= 0">
   <xsl:text>+</xsl:text>
 </xsl:if>
 <xsl:value-of select="$exp"/>

</xsl:template>

<!--
  This is the body of the normalizer. It recursively
  processes the number until it's in the correct form
-->
<xsl:template name="do-normalize">

 <xsl:param name="mantissa" select="0"/>
 <xsl:param name="exp" select="0"/>

 <xsl:choose>

   <!-- If the mantissa is 0, we're done. Just print it out -->
   <xsl:when test="$mantissa = 0.0 or $mantissa = -0.0">
     <xsl:value-of select="format-number(0.0, '#,##0.00000')"/>
     <xsl:text>e</xsl:text>
     <xsl:call-template name="format-exp">
       <xsl:with-param name="exp" select="$exp"/>
     </xsl:call-template>
   </xsl:when>

   <!-- If we've gotten the number normalized, then print it out.
        This is for a positive number -->
   <xsl:when test="$mantissa &gt;= 1.0 and $mantissa &lt; 10.0">
     <xsl:value-of select="format-number (number($mantissa),
'#,##0.00000')"/>
     <xsl:text>e</xsl:text>
     <xsl:call-template name="format-exp">
       <xsl:with-param name="exp" select="$exp"/>
     </xsl:call-template>
   </xsl:when>

   <!-- We've gotten a negative number normalized, print it -->
   <xsl:when test="$mantissa &gt; -10.0 and $mantissa &lt;= -1.0">
     <xsl:value-of select="format-number (number($mantissa),
'#,##0.00000')"/>
     <xsl:text>e</xsl:text>
     <xsl:call-template name="format-exp">
       <xsl:with-param name="exp" select="$exp"/>
     </xsl:call-template>
   </xsl:when>

   <!-- In this case we've got a number greater than abs(10.0). We
        add one to the exponent, divide the mantissa by 10, and recurse -->
   <xsl:when test="$mantissa &gt;= 10.0 or $mantissa &lt;= -10.0">
     <xsl:call-template name="do-normalize">
       <xsl:with-param name="mantissa" select="$mantissa div 10"/>
       <xsl:with-param name="exp" select="$exp + 1"/>
     </xsl:call-template>
   </xsl:when>

   <!-- In this case we have a number whose abs() < 1.0. We subtract 1
        from the exponent, multiply the mantissa by 10 and recurse. -->
   <xsl:when test="$mantissa &gt; -1.0 and $mantissa &lt; 1.0">
     <xsl:call-template name="do-normalize">
       <xsl:with-param name="mantissa" select="$mantissa * 10"/>
       <xsl:with-param name="exp" select="$exp - 1"/>
     </xsl:call-template>
   </xsl:when>
 </xsl:choose>

</xsl:template>
</xsl:stylesheet>

output:

[ejm@localhost xslt]$ java  com.icl.saxon.StyleSheet sample.xml sample.xsl
<?xml version="1.0" encoding="utf-8"?>
4.00000e-8
1.23456e+5
-7.56444e+5

Edmund



-----Original Message-----
From: Tim Watts [mailto:timw@xxxxxxx]

Edward,

Have you looked into using the format-number function?
Regards, Tim Watts




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread