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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number to text 3

Status
Not open for further replies.

Brian56

Programmer
May 29, 2003
66
CA
Hi All,

This is probably something I have missed and is very simple. How do I convert a number to text ?

eg. textfield value is 123 and i want it to display one hundred and twenty three.

There must be a built-in function to do this but I just cannot find it.


Brian
 
Usually it's the reverse? Textfields usually hold strings...

Regards,

cubalibre2.gif
 
No built-in function that I can think of. You could try something with arrays:

myNumber = 123;
lastDigitArray = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
remainder = myNumber%10;
lastDigit = lastDigitArray[remainder];
trace(lastDigit);

...you'll need an array for the tens, twenties etc and a bit of logic to format things correctly (no 'twenty zero' for example)...
 
Thanks Wangbar,

That looks promising and I will try to develop that idea.

I had assumed that flash would have a built in function like the excel function that does this.

I will give your idea a go and let you know how I get on.

Brian
 
Thanks Wangbar, your idea is working out well. Have a star.

This is what I have so far and I can extend it further but I see another problem on the horizon. Sometimes the result will be a decimal (dollars and cents) and this method will not work.

Code:
units=["","one","two","three","four","five","six","seven","eight","nine"];
teens=["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"];
tens=["","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"];
answer = 147;
numbers = answer.toString().split("");
switch (numbers.length){
	case 1:
	asText.text = units[numbers[0]];
	break;
	case 2:
	if(numbers[0]==1)
	asText.text = teens[numbers[1]]
	else 
	asText.text = tens[numbers[0]] + " " + units[numbers[1]]
	break;
	case 3:
	asText.text = units[numbers[0]] + " hundred";
	if(numbers[1]==1)
	asText.text += " and " + teens[numbers[1]]
	else 
	asText.text += " and " + tens[numbers[1]] + " " + units[numbers[2]]
	break;
}

Do you have another bright idea to get me out of this hole?

Thanks, Brian
 
you almost had it...just needs another couple of splits

Code:
units=["","one","two","three","four","five","six","seven","eight","nine"];
teens=["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"];
tens=["","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"];
answer = 904.20;
numbers = answer.toString().split(".");
result = numbers[0].split("")
switch (result.length){
	case 1:
	asText.text = units[result[0]];
	break;
	case 2:
	if(result[0]==1)
	asText.text = teens[result[1]]
	else 
	asText.text = tens[result[0]] + " " + units[result[1]]
	break;
	case 3:
	asText.text = units[result[0]] + " hundred";
	if(result[1]==1)
	asText.text += " and " + teens[result[1]]
	else 
	asText.text += " and " + tens[result[1]] + " " + units[result[2]]
	break;
}
remainder = numbers[1].split("");
if(remainder[0]==1)
	asText.text += " and " + teens[remainder[1]] + " cents";
else
	asText.text += " and " + tens[remainder[0]] + " " + units[remainder[1]] + " cents";
{/code]
 
Thats it, Bill. Many thanks to both you and Wangbar for your time and expert help.

My actionscript is improving by the day but I often need you guys to point out the way. The solutions are usually obvious once pointed out.

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top