# Statistics

Viewed 1225 times
Favorited by 0 user(s)

# AS3 Convert a Number to a String

/ Published in: ActionScript 3

// Example usage ...

trace(fNumToWord(56345));

// Output ...
// Fifty-Six Thousand and Three Hundred and Forty-Five

Copy this code and paste it in your HTML
`function fNumToWord(vNum:int):String{	var mWords:Array =  ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty"];	mWords[30] = "Thirty";	mWords[40] = "Forty";	mWords[50] = "Fifty";	mWords[60] = "Sixty";	mWords[70] = "Seventy";	mWords[80] = "Eighty";	mWords[90] = "Ninety"; 	var v : int;	var vWord : String;	var vString : String; 	vWord = "";		if (vNum < 21) {		vWord += mWords[vNum];	}else if (vNum < 100) {		vWord += mWords[10 * Math.floor(vNum / 10)];		v = vNum % 10;		vString = (String(vNum).substr( -1) == "0") ? " " : "-";		if (v > 0) {			vWord += vString + mWords[v];		}	} else if (vNum < 1000) {		vString = (String(vNum).substr( -2) == "00") ? " Hundred" : " Hundred and";				vWord += mWords[Math.floor(vNum / 100)] + vString;		v = vNum % 100;				if (v > 0) {			vWord += " " + fNumToWord(v);		}	} else if (vNum < 1000000) {		vString = (String(vNum).substr( -3) == "000") ? " Thousand" : " Thousand and";		vWord += fNumToWord(Math.floor(vNum / 1000)) + vString;		v = vNum % 1000;		if (v > 0) {			vWord += " ";			if (v < 100)				vWord += " ";			vWord += fNumToWord(v);		}	} else {		vString = (String(vNum).substr( -6) == "000000") ? " Million" : " Million and";		vWord += fNumToWord(Math.floor(vNum / 1000000)) + vString;		v = vNum % 1000000;		if (v > 0) {			vWord += " ";			if (v < 100) {				vWord += " ";			}			vWord += fNumToWord(v);		 }	 }	return vWord;}`