xsl-list
[Top] [All Lists]

convert integers to words

2004-09-17 12:25:43
NOTE: Just for the record, there were a couple defects which are now fixed.

Hello all,
 Anyone ever post something like this?  I haven't really been paying
very close attention lately.
 It's a coupla stylesheets that convert integers to words.  The first
is simply to call the main one and spit out an xsl:message.  It takes
one param, the number you want to convert and can be in the range 1 < x
< 10E66.
 Personally, I needed it to convert some number <100.  The rest was
just for fun.

Matthew L. Avizinis
mla at gleim dot com
Gleim Publications, Inc.
_________________________________________________________________________________________

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xsl:import href="int2word.xslt"/>
   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
   <xsl:param name="number"/>
   <xsl:template match="/">
      <xsl:variable name="num-in-words">
        <xsl:call-template name="int2word">
          <xsl:with-param name="in-integer" select="$number"/>
        </xsl:call-template>
      </xsl:variable>
<xsl:message><xsl:text>
</xsl:text>### <xsl:value-of select="$number"/> in words : <xsl:value-of
select="$num-in-words"/> ###<xsl:text> </xsl:text></xsl:message>
   </xsl:template>
</xsl:stylesheet>


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

<xsl:variable name="int2word" select=" '1One|2Two|3Three|4Four|5Five|6Six|7Seven|8Eight|9Nine|10Ten|11Eleven|12Twelve|13Thirteen|14Fourteen|15Fifteen|16Sixteen|17Seventeen|18Eighteen|19Nineteen|' "/> <xsl:variable name="tens2word" select=" '2Twenty|3Thirty|4Forty|5Fifty|6Sixty|7Seventy|8Eighty|9Ninety|' "/> <xsl:variable name="thousands2word" select=" '3Thousand|6Million|9Billion|12Trillion|15Quadrillion|18Quintillion|21Sextillion|24Septillion|27Octillion|30Nonillion|33Decillion|36Undecillion|39Duodecillion|42Tredecillion|45Quattuordecillion|48Quindecillion|51Sexdecillion|54Septendecillion|57Octodecillion|60Novemdecillion|63Vigintillion|' "/>

  <xsl:template name="int2word">
    <xsl:param name="in-integer" select="1"/>
    <!-- this is the number you want to convert to a word or words -->
<xsl:variable name="the-number" select="translate($in-integer, ',.', '')"/>
    <!-- remove any formatting characters -->
    <xsl:variable name="num-length" select="string-length($the-number)"/>
    <xsl:variable name="group-length">
      <xsl:choose>
        <xsl:when test="($num-length mod 3) = 0">3</xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$num-length mod 3"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
<xsl:variable name="first-group" select="substring($the-number, 1, $group-length)"/> <xsl:variable name="the-rest" select="substring($the-number, $group-length + 1, $num-length)"/>
    <xsl:choose>
      <xsl:when test="not($the-rest = '')">
        <xsl:call-template name="hundreds2words">
          <xsl:with-param name="group" select="$first-group"/>
        </xsl:call-template>
        <xsl:if test="number($first-group)">
<xsl:value-of select="concat(substring-before(substring-after($thousands2word, string-length($the-rest)), '|'),' ')"/>
        </xsl:if>
        <xsl:if test="number($the-rest)">
           <xsl:call-template name="int2word">
             <xsl:with-param name="in-integer" select="$the-rest"/>
           </xsl:call-template>
        </xsl:if>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="hundreds2words">
          <xsl:with-param name="group" select="$first-group"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
<!--Every group of three in American numbering is the basis for counting hundreds of - - thousands, millions, etc. -->
  <xsl:template name="hundreds2words">
    <xsl:param name="group"/>
<xsl:variable name="first-digit" select="substring(number($group), 1, 1)"/>
    <xsl:variable name="remaining-digits">
       <xsl:choose>
<xsl:when test="(19 &lt; number($group)) and (number($group) &lt; 100) "><xsl:value-of select="substring(number($group), 2, 1)"/></xsl:when> <xsl:when test="(99 &lt; number($group)) and (number($group) &lt; 1000) "><xsl:value-of select="substring(number($group), 2, 2)"/></xsl:when>
      </xsl:choose>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="not($remaining-digits = '')">
          <xsl:choose>
            <xsl:when test="string-length($remaining-digits) = 1">
<xsl:value-of select="concat(substring-before(substring-after($tens2word, $first-digit), '|'), ' ')"/>
            </xsl:when>
            <xsl:when test="string-length($remaining-digits) = 2">
<xsl:value-of select="concat(substring-before(substring-after($int2word, $first-digit), '|'), ' ')"/>
              <xsl:value-of select=" 'Hundred ' "/>
            </xsl:when>
          </xsl:choose>
          <xsl:call-template name="hundreds2words">
<xsl:with-param name="group" select="number($remaining-digits)"/>
          </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
         <xsl:choose>
           <xsl:when test="not(number($first-digit))"/>
           <xsl:otherwise>
<xsl:value-of select="concat(substring-before(substring-after($int2word, number($group)), '|'), ' ')"/>
           </xsl:otherwise>
         </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>



<Prev in Thread] Current Thread [Next in Thread>