Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 2.50 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-21-2002, 11:21 PM   PM User | #1
rawsweets
New Coder

 
Join Date: Aug 2002
Location: Oakland, CA
Posts: 45
Thanks: 1
Thanked 0 Times in 0 Posts
rawsweets is an unknown quantity at this point
Macintosh Number to word translator

Hey folks,

Don't know if there is one of these online, so I made me own. Simple function that outputs a number as words. First argument is the number, second argument is flag (default = false) that controls whether output is cardinal or ordinal - as in "one" or "first".

Goodness knows if I have the grammer correct, especially when the numbers get big. Enjoy!

LIMITS:
1) Doesn't work with WebTV or below NS 4. Changing the large literal array into a common array should fix that, but I didn't bother.
2) Converts number into an integer before wording number. So, no "one point forty-five" stuff. This function is a good start for anyone who wants to do that though.
3) Always returns, "zero" for 0. Couldn't find cardinal equivalent to "zero"... zeroeth?

Code:
/*
Num2Word, ouputs written number in human language format
ARGUMENTS:
-----------
Nbr: num, the number to be worded. This number is converted into an integer.
Crd: bol, flags whether output is cardinal or ordinal number. Cardinal is used when describing the order of items, like "first" or "second" place.
*/
function Num2Word(num,fmt) {
//_ arguments
	num = Math.round(num).toString(); // round value
	if (num == 0) return 'zero'; // if number given is 0, return and exit
//_ locals
	// word numbers
	var wnums = [['hundred','thousand','million','billion','trillion','zillion'],['one','first','ten','','th'],['two','second','twen',0,0],['three','third','thir',0,0],['four','fourth',0,0,0],['five','fifth','fif',0,0],['six','sixth',0,0,0],['seven','seventh',0,0,0],['eight','eighth','eigh',0,0],['nine','ninth',0,0,0],['ten',],['eleven',],['twelve','twelfth'],['thirteen',],['fourteen',],['fifteen',],['sixteen',],['seventeen',],['eighteen',],['nineteen',]];
	// digits outside triplets
	var dot = (num.length % 3) ? num.length % 3 : 3;
	// number of triplets in number
	var sets = Math.ceil(num.length/3);
	// result string, word as number
	var rslt = '';
//_ convert every three digits
	for (var i = 0; i < sets; i++) { // for each set of triplets
		// capture set of numbers up to three digits
		var subt = num.substring((!i) ? 0 : dot + (i - 1) * 3,(!i) ? dot : dot + i * 3);
		if (subt != 0) { // if value of set is not 0...
			var hdec = (subt.length > 2) ? subt.charAt(0) : 0; // get hundreds digit
			var ddec = subt.substring(Math.max(subt.length - 2,0),subt.length); // get double digits
			var odec = subt.charAt(subt.length - 1); // get one's digit
			// hundreds digit
			if (hdec != 0) rslt += ' ' + wnums[hdec][0] + '-hundred ' + ((fmt && ddec == 0) ? 'th' : '');
			// add double digit
			if (ddec < 20 && 9 < ddec) { // if less than 20...
				// add double digit word
				rslt += ' ' + ((fmt) ? ((wnums[ddec][1]) ? wnums[ddec][1] : wnums[ddec][0] + 'th') : wnums[ddec][0]);
			} else { // otherwise, add words for each digit...
				// add "and" for last set without a tens digits
				if ((0 < hdec || 1 < sets) && i + 1 == sets && 0 < ddec && ddec < 10) rslt += 'and ';
				// tens digit
				if (19 < ddec) rslt += wnums[ddec.charAt(0)][(wnums[ddec.charAt(0)][2]) ? 2 : 0] + ((i + 1 == sets && odec == 0 && fmt) ? 'tieth' : 'ty') + ((0 < odec) ? '-' : ' ');
				// one's digit
				if (0 < odec) rslt += wnums[odec][(i + 1 == sets && fmt) ? 1 : 0];
			}
			// add place for set
			if (i + 1 < sets) rslt += ' ' + wnums[0][sets - i - 1] + ' '; // if not last set, add place
		} else if (i + 1 == sets && fmt) { // otherwise, if this set is zero, is the last set of the loop, and the format (fmt) is cardinal
			rslt += 'th'; // add cardinal "th"
		}
	}
//_ return result
	return rslt
}
Test the result of various valies like so

Code:
for (var i = 0; i < 50; i++) {
    var x = Math.round(Math.random() * 5000000);
    document.write(x,' -> ' + Num2Word(x).bold(),'<BR>');
}
Use this function towards whatever means you deem necessary. Pease credit me, Bemi Faison, if you use this script.

- Much obliged.

rawsweets

Last edited by rawsweets; 08-22-2002 at 05:36 PM..
rawsweets is offline   Reply With Quote
Old 08-22-2002, 03:31 PM   PM User | #2
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
Cool. Like it.
Works great except when x == 1-9, then it displays

1 -> and one
2 -> and two
3 -> and three
etc....
beetle is offline   Reply With Quote
Old 08-22-2002, 05:08 PM   PM User | #3
rawsweets
New Coder

 
Join Date: Aug 2002
Location: Oakland, CA
Posts: 45
Thanks: 1
Thanked 0 Times in 0 Posts
rawsweets is an unknown quantity at this point
Macintosh

Updated original post to address that.

Thanks for the comment and customize as needed. :-)

Last edited by rawsweets; 08-22-2002 at 05:37 PM..
rawsweets is offline   Reply With Quote
Old 08-22-2002, 05:12 PM   PM User | #4
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
Ugh. Ya, lots to be done there. I, however, don't have THAT much free time

Keep up the good work
beetle is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:00 AM.


Advertisement
Log in to turn off these ads.