RE: [xsl] XSL : Numbers to Words

Subject: RE: [xsl] XSL : Numbers to Words
From: "Josh Canfield" <Josh.Canfield@xxxxxxxxxxxx>
Date: Fri, 23 Apr 2004 10:22:14 -0700
There is probably a better way to do this, but here is my attempt... It will translate up to trillion, (999999999999999) after that it starts putting in the multiple instead of the name (1000000000000000). If I were going to do it again I might start by breaking the number string up into pieces so that math operations on large numbers doesn't limit the size of number supported... 

Enjoy,
Josh

===== xml.xml =====

<?xml version="1.0" encoding="ISO-8859-1"?>

<a>
  <num>12345678901234</num>
</a>

===== Output =====

<?xml version="1.0" encoding="utf-8"?>
<a>
   <numInWords>twelve trillion three hundred and forty-five billion six hundred
and seventy-eight million nine hundred and one thousand two hundred and thirty-f
our</numInWords>
</a>

===== num-to-words.xsl =====

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:output method="xml" indent="yes" encoding="utf-8"/>


  <xsl:template match="/">
    <a><numInWords>
    <xsl:call-template name="num-to-word">
      <xsl:with-param name="value" select="a/num"/>
    </xsl:call-template>
    </numInWords></a>
  </xsl:template>

  <xsl:template name="num-to-word">
    <xsl:param name="value"/>
    <xsl:param name="multiple"/>

    <xsl:variable name="m">
      <xsl:choose>
        <xsl:when test="$multiple">
          <xsl:value-of select="$multiple"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="get-multiple">
            <xsl:with-param name="value" select="$value"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:choose>
      
      <!-- partition, thousands, millions, billions, etc -->
      <xsl:when test="$value &gt; 9999">

        <xsl:variable name="part" select="floor($value div $m)"/>
        <xsl:variable name="r" select="$value - ($part * $m)"/>

        <xsl:call-template name="num-to-word">
          <xsl:with-param name="value" select="$part"/>
        </xsl:call-template>

        <xsl:text> </xsl:text><xsl:call-template name="word-map"><xsl:with-param name="val" select="$m"/></xsl:call-template>
        <xsl:if test="$r">
          <xsl:text> </xsl:text>
          <xsl:call-template name="num-to-word">
            <xsl:with-param name="value" select="$r"/>
          </xsl:call-template>
        </xsl:if>

      </xsl:when>

      <!-- special case the teens -->
      <xsl:when test="$value &lt; 19">
        <xsl:call-template name="word-map">
          <xsl:with-param name="val" select="$value"/>
        </xsl:call-template>
      </xsl:when>

      <!-- handles values 20 - 9999 -->
      <xsl:otherwise>

        <xsl:variable name="part" select="floor($value div $m)"/>
        <xsl:variable name="r" select="$value - ($part * $m)"/>

        <xsl:choose>
          <xsl:when test="$m=10 and $part &gt; 1">
            <xsl:call-template name="word-map">
              <xsl:with-param name="val" select="$part*$m"/>
            </xsl:call-template>
          </xsl:when>

          <xsl:otherwise>
            <xsl:call-template name="word-map">
              <xsl:with-param name="val" select="$part"/>
            </xsl:call-template>

            <!-- add hundred, thousand, million, etc -->
            <xsl:if test="$m &gt; 10">
              <xsl:text> </xsl:text><xsl:call-template name="word-map"><xsl:with-param name="val" select="$m"/></xsl:call-template>
            </xsl:if>

          </xsl:otherwise>
        </xsl:choose>

        <xsl:if test="$r">

          <xsl:choose>
            <!-- for: one hundred and five -->
            <xsl:when test="$m = 100"><xsl:text> and </xsl:text></xsl:when>
            <!-- for: thirty-five -->
            <xsl:when test="$m = 10"><xsl:text>-</xsl:text></xsl:when>
            <!-- for: one thousand one hundred -->
            <xsl:otherwise><xsl:text> </xsl:text></xsl:otherwise>
          </xsl:choose>

          <!-- translate the next part -->
          <xsl:call-template name="num-to-word">
            <xsl:with-param name="value" select="$r"/>
          </xsl:call-template>

        </xsl:if> 

      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>

  <!-- determine the multiple for a number -->
  <xsl:template name="get-multiple">
    <xsl:param name="value"/>
    <xsl:param name="multiple" select="1"/>
    <xsl:param name="inc" select="10"/>
    <xsl:choose>
      <!-- at the thousand mark start multiplying by 1000 -->
      <xsl:when test="$value &gt; 999">
        <xsl:call-template name="get-multiple">
          <xsl:with-param name="value" select="floor($value div 1000)"/>
          <xsl:with-param name="multiple" select="$multiple * 1000"/>
          <xsl:with-param name="inc" select="1000"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$value &gt; $inc">
        <xsl:call-template name="get-multiple">
          <xsl:with-param name="value" select="floor($value div $inc)"/>
          <xsl:with-param name="multiple" select="$multiple * $inc"/>
          <xsl:with-param name="inc" select="$inc"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$multiple"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="word-map">
    <xsl:param name="val"/>
    <xsl:choose>
      <xsl:when test="$val = 1">one</xsl:when>
      <xsl:when test="$val = 2">two</xsl:when>
      <xsl:when test="$val = 3">three</xsl:when>
      <xsl:when test="$val = 4">four</xsl:when>
      <xsl:when test="$val = 5">five</xsl:when>
      <xsl:when test="$val = 6">six</xsl:when>
      <xsl:when test="$val = 7">seven</xsl:when>
      <xsl:when test="$val = 8">eight</xsl:when>
      <xsl:when test="$val = 9">nine</xsl:when>
      <xsl:when test="$val = 10">ten</xsl:when>
      <xsl:when test="$val = 11">eleven</xsl:when>
      <xsl:when test="$val = 12">twelve</xsl:when>
      <xsl:when test="$val = 13">thirteen</xsl:when>
      <xsl:when test="$val = 14">fourteen</xsl:when>
      <xsl:when test="$val = 15">fifteen</xsl:when>
      <xsl:when test="$val = 16">sixteen</xsl:when>
      <xsl:when test="$val = 17">seventeen</xsl:when>
      <xsl:when test="$val = 18">eighteen</xsl:when>
      <xsl:when test="$val = 19">nineteen</xsl:when>
      <xsl:when test="$val = 20">twenty</xsl:when>
      <xsl:when test="$val = 30">thirty</xsl:when>
      <xsl:when test="$val = 40">forty</xsl:when>
      <xsl:when test="$val = 50">fifty</xsl:when>
      <xsl:when test="$val = 60">sixty</xsl:when>
      <xsl:when test="$val = 70">seventy</xsl:when>
      <xsl:when test="$val = 80">eighty</xsl:when>
      <xsl:when test="$val = 90">ninety</xsl:when>
      <xsl:when test="$val = 100">hundred</xsl:when>
      <xsl:when test="$val = 1000">thousand</xsl:when>
      <xsl:when test="$val = 1000000">million</xsl:when>
      <xsl:when test="$val = 1000000000">billion</xsl:when>
      <xsl:when test="$val = 1000000000000">trillion</xsl:when>
      <xsl:otherwise><xsl:value-of select="$val"/></xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

-----Original Message-----
From: Renganat Krishnan [mailto:renganat_krishnan@xxxxxxxxx]
Sent: Thursday, April 22, 2004 2:11 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] XSL : Numbers to Words


I was wondering if anyone has acheived this

Converting a number to Words in XSL. 

Xample

<a>
 <num>10000</num>
</a>
 output from xsl on this xml should be
<a>
 <numInWords>ten thousand</numInWords>
</a>

thanks!
Krishnan


	
		
__________________________________
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25"
http://photos.yahoo.com/ph/print_splash

Current Thread