Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I translate a number into words

Status
Not open for further replies.

serarmoro

Programmer
Dec 18, 2002
16
MX
I need to translate numbers into words for billing purposes. I've tried several ways but I get a long and complex code. Does anybody have a shorter or simpler one?
 
A shorter or simpler method probably not, it's complex but there is code already written for you to copy and paste on corel's website at:
ftp://ftp.corel.com/pub/Paradox/PdoxWin/UnIndexed/pdoxwin/ti/ti2049.asc

Perrin
 
It's not easy. I saw your post and whipped up a sample that converted numbers with 4 digits (single thousands) just as an exercise. It was a quickie, so watch for bugs. You could easily expand on it to interpret any size number. I will post the code below, but it might be easier to see the form and read the code, so email me if you want it:

langley_mckelvy@cd4.co.harris.tx.us

=========================================================
Imagine a form with a pushbutton and two undefined fields (startNum and numText). The code is attached to the pushbutton for simplicity.

code attached to pushbutton method

Code:
var
 theNum     longInt
 Snum       string
 textNum    string
 teenSwitch	number
 endvar

teenSwitch = 0
theNum = startNum.value 
textNum = "Zero"
Snum = string(theNum); convert to string


if sizeEx(theNum) = 4 ; deals with thousands
  then  	textNum = getNum(Snum.substr(1,1),"one")+" Thousand"
         if getNum(Snum.substr(2,1),"one") = "Zero"
         	then	textNum = textNum
            	else	textNum = textNum+" "+getNum(Snum.substr(2,1),"one")+" Hundred"
         endif
         if getNum(Snum.substr(3,1),"ten") = "Zero"
         	then	textNum = textNum
         endif
         if getNum(Snum.substr(3,1),"ten") = "One"
         	then	textNum = textNum
            		teenSwitch = 1
         endif
			if teenSwitch = 1
         	then textNum = textNum+" "+getNum(Snum.substr(4,1),"teen")
                 numText.value = textnum
                 return
         endif
			if teenSwitch = 0
         	then  if getNum(Snum.substr(3,1),"ten") = "Zero"
         				then	textNum = textNum
                     	else 	textNum = textNum+" "+getNum(Snum.substr(3,1),"ten")
            		endif
         endif
			if getNum(Snum.substr(4,1),"one") = "Zero"
         	then	textNum = textNum
         		else 	textNum = textNum+" "+getNum(Snum.substr(4,1),"one")
     		endif
endif

numText.value = textnum

Here is the getNum custom method

Code:
method getNum(inNum string, pos string) string


switch

	case	inNum = "1" and pos = "one":
   			return "One"
   case  inNum = "2" and pos = "one":
   			return "Two"
	case	inNum = "3" and pos = "one":
   			return "Three"
	case	inNum = "4" and pos = "one":
   			return "Four"
	case	inNum = "5" and pos = "one":
   			return "Five"
   case  inNum = "6" and pos = "one":
   			return "Six"
	case	inNum = "7" and pos = "one":
   			return "Seven"
	case	inNum = "8" and pos = "one":
   			return "Eight"
	case	inNum = "9" and pos = "one":
   			return "Nine"
   case  inNum = "0" and pos = "one":
   			return "Zero"

endSwitch

switch

	case	inNum = "1" and pos = "ten":
   			return "One"
   case  inNum = "2" and pos = "ten":
   			return "Twenty"
	case	inNum = "3" and pos = "ten":
   			return "Thirty"
	case	inNum = "4" and pos = "ten":
   			return "Forty"
	case	inNum = "5" and pos = "ten":
   			return "Fifty"
   case  inNum = "6" and pos = "ten":
   			return "Sixty"
	case	inNum = "7" and pos = "ten":
   			return "Seventy"
	case	inNum = "8" and pos = "ten":
   			return "Eighty"
	case	inNum = "9" and pos = "ten":
   			return "Ninety"
   case  inNum = "0" and pos = "ten":
   			return "Zero"

endSwitch

switch

	case	inNum = "1" and pos = "teen":
   			return "Eleven"
   case  inNum = "2" and pos = "teen":
   			return "Twelve"
	case	inNum = "3" and pos = "teen":
   			return "Thirteen"
	case	inNum = "4" and pos = "teen":
   			return "Fourteen"
	case	inNum = "5" and pos = "teen":
   			return "Fifteen"
   case  inNum = "6" and pos = "teen":
   			return "Sixteen"
	case	inNum = "7" and pos = "teen":
   			return "Seventeen"
	case	inNum = "8" and pos = "teen":
   			return "Eighteen"
	case	inNum = "9" and pos = "teen":
   			return "Nineteen"
   case  inNum = "0" and pos = "teen":
   			return "Zero"

endSwitch

return ""

endMethod

This will probably look like crap, because I just did a cut and paste from my form. Mac :)

"There are only 10 kinds of people in this world... those who understand binary and those who don't"

langley_mckelvy@cd4.co.harris.tx.us
 
After reading Kliots post, I realise I just wasted about 30 minutes. ;) Mac :)

"There are only 10 kinds of people in this world... those who understand binary and those who don't"

langley_mckelvy@cd4.co.harris.tx.us
 
Mac,

Nah! Practice is never wasted. At least you had your idea viol--I mean--validated. ;-)

-- Lance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top