scripting@wanadoo.nl




NumberWords()

http://www.scriptlogic.com/kixtart/FunctionLibrary_FunctionList.aspx


actual FunctionLibrary list on 'Scriptlogic' site printer-friendly mirror of UDF topic on 'scripting@wanadoo.nl' site close
16 March 2003 NumberWords()
AuthorW.M. Hinsch
Action Returns the English words for a given number. I.e. 23504 is "Twenty-Three Thousand Five Hundred Four".
SyntaxNumberWords ($Number) 
Parameters 
RemarksThis is one of those functions that have pretty narrow application, but when you need it you need it. 
ReturnsA string with the English spoken representation of a given number. 
DependenciesNumbers higher than KiXtart's ~2 billion limit (31 bit) need to be submitted as a string. I.e. '59238210324' 
Examples
FOR $i=0 TO 12
 $Num=Rnd()
 ''+$Num+'='+NumberWords($Num) ?
 $Num=$Num*Rnd()
 ''+$Num+'='+NumberWords($Num) ?
NEXT

41=Forty-One
757147=Seven Hundred Fifty-Seven Thousand One Hundred Forty-Seven
6334=Six Thousand Three Hundred Thirty-Four
167851000=One Hundred Sixty-Seven Million Eight Hundred Fifty-One Thousand
19169=Nineteen Thousand One Hundred Sixty-Nine
301413356=Three Hundred One Million Four Hundred Thirteen Thousand Three Hundred Fifty-Six
11478=Eleven Thousand Four Hundred Seventy-Eight
336971124=Three Hundred Thirty-Six Million Nine Hundred Seventy-One Thousand One Hundred Twenty-Four
26962=Twenty-Six Thousand Nine Hundred Sixty-Two
659598368=Six Hundred Fifty-Nine Million Five Hundred Ninety-Eight Thousand Three Hundred Sixty-Eight
5705=Five Thousand Seven Hundred Five
160567225=One Hundred Sixty Million Five Hundred Sixty-Seven Thousand Two Hundred Twenty-Five
23281=Twenty-Three Thousand Two Hundred Eighty-One
391749387=Three Hundred Ninety-One Million Seven Hundred Forty-Nine Thousand Three Hundred Eighty-Seven
9961=Nine Thousand Nine Hundred Sixty-One
4890851=Four Million Eight Hundred Ninety Thousand Eight Hundred Fifty-One
2995=Two Thousand Nine Hundred Ninety-Five
35766290=Thirty-Five Million Seven Hundred Sixty-Six Thousand Two Hundred Ninety
4827=Four Thousand Eight Hundred Twenty-Seven
26239572=Twenty-Six Million Two Hundred Thirty-Nine Thousand Five Hundred Seventy-Two
32391=Thirty-Two Thousand Three Hundred Ninety-One
473038164=Four Hundred Seventy-Three Million Thirty-Eight Thousand One Hundred Sixty-Four
3902=Three Thousand Nine Hundred Two
597006=Five Hundred Ninety-Seven Thousand Six
292=Two Hundred Ninety-Two
3615544=Three Million Six Hundred Fifteen Thousand Five Hundred Forty-Four

 
Source
FUNCTION NumberWords ($number)
  ; Given a number, returns a word string
  ; I.e. 536 = "Five Hundred Thirty-Six"
  DIM $i, $stmp1, $stmp2, $sint, $sfrc, $aint, $afrc, $idigits
  REDIM $adigs[9]
  REDIM $atens[9]
  $adigs='Zero','One','Two','Three','Four','Five','Six','Seven','Eight','Nine'
  $ateens='Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen'
  $atens='','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'

  $number=''+$number ; Convert to string
  IF $number = '000' OR $number='00'
    EXIT
  ENDIF
  IF InStr($number,'.')
    $sint=Substr($number,1,InStr($number,'.')-1)
    $sfrc=Substr($number,InStr($number,'.')+1)
  ELSE
    $sint=Substr($number,1)
    $sfrc=''
  ENDIF
  FOR $i = 0 TO Len($sint) - 1
    REDIM PRESERVE $aint[$i]
    $aint[$i]=CInt(Substr($sint,Len($sint)-$i,1))
  NEXT
  $idigits=UBound($aint)+1
  FOR $i = 1 TO Len($sfrc)
    REDIM PRESERVE $afrc[$i-1]
    $afrc[$i-1]=CInt(Substr($sfrc,$i,1))
  NEXT
  SELECT
  CASE ($idigits = 1)
    $stmp2=$adigs[$aint[0]]
  CASE ($idigits = 2)
    $stmp1=Right($sint,2)
    SELECT
    CASE ($aint[1] = 0) ; Tens digit is 0
      $stmp2=''+$adigs[$aint[0]]
    CASE ($aint[1] = 1) ; Tens digit is 1
      $stmp2=$ateens[$aint[0]]
    CASE 1 ; Tens digit is 2-9
      $stmp2=$atens[$aint[1]]
      IF ($aint[0] <> 0)
        $stmp2=$stmp2+'-'+$adigs[$aint[0]]
      ENDIF
    ENDSELECT
  CASE ($idigits = 3)
    ;$sTmp1=SubStr($sInt,Len($sInt)-3,3)
    IF ($aint[2] = 0) ; Hundreds place is 0
      $stmp2=numberwords(Right($sint,2))
    ELSE
      $stmp2=''+numberwords($aint[2])+' Hundred '
      IF ($aint[1] = 0) ; Tens place is zero
        IF ($aint[0] <> 0) ; Ones place is not zero
          $stmp2=$stmp2+numberwords(Right($sint,1))
        ENDIF
      ELSE
        $stmp2=$stmp2+numberwords(Right($sint,2))
      ENDIF
    ENDIF
  ENDSELECT
  SELECT
  CASE (UBound($aint) > 2) AND (UBound($aint) < 6)
    $stmp2=numberwords(Substr($sint,1,Len($sint)-3))+' Thousand '
    IF (Right($sint,3) <> '000')
      $stmp2=$stmp2+numberwords(CInt(Right($sint,3)))
    ENDIF
  CASE (UBound($aint) > 5) AND (UBound($aint) < 9)
    $stmp2=numberwords(Substr($sint,1,Len($sint)-6))+' Million '
    IF (Right($sint,6) <> '000000')
      $stmp2=$stmp2+numberwords(CInt(Right($sint,6)))
    ENDIF
  CASE (UBound($aint) > 8) AND (UBound($aint) < 12)
    $stmp2=numberwords(Substr($sint,1,Len($sint)-9))+' Billion '
    IF (Right($sint,9) <> '000000000')
      $stmp2=$stmp2+numberwords(CInt(Right($sint,9)))
    ENDIF
  CASE (UBound($aint) > 11) AND (UBound($aint) < 15)
    $stmp2=numberwords(Substr($sint,1,Len($sint)-12))+' Trillion '
    IF (Right($sint,12) <> '000000000000')
      $stmp2=$stmp2+numberwords(CInt(Right($sint,12)))
    ENDIF
  CASE (UBound($aint) > 14) AND (UBound($aint) < 18)
    $stmp2=numberwords(Substr($sint,1,Len($sint)-15))+' Quadrillion '
    IF (Right($sint,15) <> '000000000000000')
      $stmp2=$stmp2+numberwords(CInt(Right($sint,15)))
    ENDIF
  ENDSELECT
  IF (UBound($afrc) >= 0)
    $stmp2=$stmp2 + ' Point '
    FOR EACH $i IN $afrc
      $stmp2=$stmp2 + ' ' + numberwords($afrc[$i])
    NEXT
  ENDIF
  $numberwords=Trim($stmp2)
ENDFUNCTION ; - NumberWords -
 
  original source of UDF topic. show actual FunctionLibrary list on Scriptlogic site close top
          printer-friendly mirror of UDF topic on scripting@wanadoo.nl site  




Copyright © 2003 www.scriptlogic.com & scripting@wanadoo.nl - last updated on 20 May 2003


Site Meter