Tech Tip: Utility method for converting numbers to text
PRODUCT: 4D | VERSION: 12.3 | PLATFORM: Mac & Win
Published On: December 9, 2011
The utility method below will take a whole number and convert it to its text equivilant. The method honors the grammatic rule of using a hyphen between the tens and units number when writing out the numbers twenty-one to ninety-nine in words. For example UTIL_NumberToText(123456) returns the text "One Hundred Twenty-Three Thousand Four Hundred Fifty-Six."
This method is also an excellent example of how to use "recursion", a method that calls itself.
If (True) If (False) //************************************ // // UTIL_NumberToText($Number_R) -> $NumberAsText_T // // Purpose: This method will convert a number to text // // Example: UTIL_NumberToText(199) -> One Hundred Ninety-Nine // // $0 - TEXT - Number as text // $1 - REAL - Number to convert // //************************************ End if C_TEXT($MethodName_T) $MethodName_T:=Current method name //===================== Declare Variables ================================== //method_parameters_declarations C_TEXT($0;$NumberAsText_T) C_REAL($1;$Number_R) //--------------------------------------------------- //local_variable_declarations C_LONGINT($SpaceCnt_L;$Params_L) C_TEXT($Blank_T;$Space_T) End if //===== Initialize and Setup ====== $Blank_T:="" $Space_T:=" " $Number_R:=$1 $NumberAsText_T:=$Blank_T //===== Method Actions ===== If ($Number_R>=1000000000) $NumberAsText_T:=$NumberAsText_T+(Num($NumberAsText_T#$Blank_T)*$Space_T))\ +UTIL_NumberToText ($Number_R\1000000000)+" Billion" $Number_R:=$Number_R%1000000000 End if If ($Number_R>=1000000) $NumberAsText_T:=$NumberAsText_T+(Num($NumberAsText_T#$Blank_T)*$Space_T))\ +UTIL_NumberToText ($Number_R\1000000)+" Million" $Number_R:=$Number_R%1000000 End if If ($Number_R>=1000) $NumberAsText_T:=$NumberAsText_T+(Num($NumberAsText_T#$Blank_T)*$Space_T))\ +UTIL_NumberToText ($Number_R\1000)+" Thousand" $Number_R:=$Number_R%1000 End if If ($Number_R>=100) $NumberAsText_T:=$NumberAsText_T+(Num($NumberAsText_T#$Blank_T)*$Space_T))\ +UTIL_NumberToText ($Number_R\100)+" Hundred" $Number_R:=$Number_R%100 End if If ($Number_R>19) $SpaceCnt_L:=Num($NumberAsText_T#$Blank_T) Case of : (($Number_R\10)=9) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Ninety" : (($Number_R\10)=8) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Eighty" : (($Number_R\10)=7) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Seventy" : (($Number_R\10)=6) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Sixty" : (($Number_R\10)=5) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Fifty" : (($Number_R\10)=4) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Forty" : (($Number_R\10)=3) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Thirty" : (($Number_R\10)=2) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Twenty" End case $Number_R:=$Number_R-(($Number_R\10)*10) If (($Number_R>0) & ($Number_R<10)) $NumberAsText_T:=$NumberAsText_T+"-" End if End if $SpaceCnt_L:=Num($NumberAsText_T#$Blank_T) Case of : ($Number_R=19) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Nineteen" : ($Number_R=18) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Eighteen" : ($Number_R=17) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Seventeen" : ($Number_R=16) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Sixteen" : ($Number_R=15) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Fifteen" : ($Number_R=14) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Fourteen" : ($Number_R=13) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Thirteen" : ($Number_R=12) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Twelve" : ($Number_R=11) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Eleven" : ($Number_R=10) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Ten" : ($Number_R=9) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Nine" : ($Number_R=8) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Eight" : ($Number_R=7) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Seven" : ($Number_R=6) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Six" : ($Number_R=5) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Five" : ($Number_R=4) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Four" : ($Number_R=3) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Three" : ($Number_R=2) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Two" : ($Number_R=1) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"One" : ($Number_R=0) If ($NumberAsText_T=$Blank_T) $NumberAsText_T:=$NumberAsText_T+($SpaceCnt_L*$Space_T))+"Zero" End if End case //===== Clean up and Exit ===== $NumberAsText_T:=Replace string($NumberAsText_T;"- ";"-") $0:=$NumberAsText_T |