[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] convert integers to words
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: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="number($the-rest)"> <xsl:call-template name="hundreds2words"> <xsl:with-param name="group" select="$first-group"/> </xsl:call-template> <xsl:choose> <xsl:when test="string-length($the-rest) = 3"> Thousand </xsl:when> <xsl:when test="string-length($the-rest) = 6"> Million </xsl:when> <xsl:when test="string-length($the-rest) = 9"> Billion </xsl:when> <xsl:when test="string-length($the-rest) = 12"> Trillion </xsl:when> <xsl:when test="string-length($the-rest) = 15"> Quadrillion </xsl:when> <xsl:when test="string-length($the-rest) = 18"> Quintillion </xsl:when> <xsl:when test="string-length($the-rest) = 21"> Sextillion </xsl:when> <xsl:when test="string-length($the-rest) = 24"> Septillion </xsl:when> <xsl:when test="string-length($the-rest) = 27"> Octillion </xsl:when> <xsl:when test="string-length($the-rest) = 30"> Nonillion </xsl:when> <xsl:when test="string-length($the-rest) = 33"> Decillion </xsl:when> <xsl:when test="string-length($the-rest) = 36"> Undecillion </xsl:when> <xsl:when test="string-length($the-rest) = 39"> Duodecillion </xsl:when> <xsl:when test="string-length($the-rest) = 42"> Tredecillion </xsl:when> <xsl:when test="string-length($the-rest) = 45"> Quattuordecillion </xsl:when> <xsl:when test="string-length($the-rest) = 48"> Quindecillion </xsl:when> <xsl:when test="string-length($the-rest) = 51"> Sexdecillion </xsl:when> <xsl:when test="string-length($the-rest) = 54"> Septendecillion </xsl:when> <xsl:when test="string-length($the-rest) = 57"> Octodecillion </xsl:when> <xsl:when test="string-length($the-rest) = 60"> Novemdecillion </xsl:when> <xsl:when test="string-length($the-rest) = 63"> Vigintillion </xsl:when> </xsl:choose> <xsl:call-template name="int2word"> <xsl:with-param name="in-integer" select="$the-rest"/> </xsl:call-template> </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 < number($group)) and (number($group) < 100) "><xsl:value-of select="substring(number($group), 2, 1)"/></xsl:when> <xsl:when test="(99 < number($group)) and (number($group) < 1000) "><xsl:value-of select="substring(number($group), 2, 2)"/></xsl:when> </xsl:choose> </xsl:variable> <xsl:choose> <xsl:when test="number($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="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="$remaining-digits"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:choose> <xsl:when test="number($group) < 20"> <xsl:value-of select="substring-before(substring-after($int2word, number($group)), '|')"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="substring-before(substring-after($int2word, $first-digit), '|')"/> </xsl:otherwise> </xsl:choose> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|