I thought I would post this routine to convert numbers to written text. It uses a recursion.
The wrapper function is just an examle of how to use it for whole numbers plus decimal parts.
Hope it is of use to someone.
Ron
Here are the two functions
The wrapper function is just an examle of how to use it for whole numbers plus decimal parts.
Hope it is of use to someone.
Ron
Here are the two functions
Code:
'------------------------------------------------------ ' Wrapper function for recursenum() ' Function to convert a number into its text equivelent ' ' Usage : send the number you want and a flag t/f ' to indicate if commas should be entered. ' ' Calls recursenum() function to do the work. ' This wrapper function is used to split a number into ' its whole and decimal parts and calls recursenum() to ' get the text ' ' This version uses a global variable addcommas to control ' the comma output from the function. '------------------------------------------------------ ' Ron McCormack '------------------------------------------------------ function numtoword as C(number_in as N,addcoms as L) '------------------------------------------------------ ' declarations '------------------------------------------------------ dim wholenum as N dim decnumas N '------------------------------------------------------ ' Split into parts '------------------------------------------------------ wholenum=int(number_in) decnum=int((number_in-wholenum)*100) '------------------------------------------------------ ' Set addcommas as required '------------------------------------------------------ var->addcommas=addcoms '------------------------------------------------------ ' Call recursenum() for whole number '------------------------------------------------------ temp1=trim(recursenum(wholenum))+" pounds" '------------------------------------------------------ ' Call recursenum() for decimal part '------------------------------------------------------ if decnum>0 temp2=" and "+trim(recursenum(decnum))+" pence" else temp2="" end if '------------------------------------------------------ ' Format return result '------------------------------------------------------ numtoword=temp1+temp2 end function '------------------------------------------------------ ' Function to convert a number into its text equivelent ' usage send the number to be converted to text ' This version uses a global variable addcommas to control ' the comma output from the function. '------------------------------------------------------ ' Ron McCormack '------------------------------------------------------ function recursenum as C(number_in as N) '------------------------------------------------------ ' Declare Variables '------------------------------------------------------ dim tmpres as C dim first_time as L '------------------------------------------------------ ' Declare Arrays '------------------------------------------------------ dim global digits[19] digits[1]="one" digits[2]="two" digits[3]="three" digits[4]="four" digits[5]="five" digits[6]="six" digits[7]="seven" digits[8]="eight" digits[9]="nine" digits[10]="ten" digits[11]="eleven" digits[12]="twelve" digits[13]="thirteen" digits[14]="fourteen" digits[15]="fifteen" digits[16]="sixteen" digits[17]="seventeen" digits[18]="eighteen" digits[19]="nineteen" dim global tens[9] tens[1]="one" tens[2]="twenty" tens[3]="thirty" tens[4]="fourty" tens[5]="fifty" tens[6]="sixty" tens[7]="seventy" tens[8]="eighty" tens[9]="ninety" SELECT CASE number_in >0 .and. number_in 19 .and. number_in 99 .and. number_in 0," and ",if(var->addcommas,", "," "))+recursenum(mod(number_in,100)) CASE number_in > 999 .and. number_in 0.and.mod(number_in,1000)addcommas,", "," "))+recursenum(mod(number_in,1000)) CASE ELSE if number_in0 tmpres="OUT OF RANGE ERROR" end if END SELECT recursenum=tmpres end function
Comment