Feb 15 2010

JavaScript Number to Text

Category: Interesting, Programmingadmin @ 4:31 am

Below is some sample JavaScript to spell an integer along with an interactive demo. It took me just over an hour. I came up with this task for use as an interview question – I think it will serve that purpose well. It’s one of those deceptively simple concept tasks – run it past some of your developer buddies and see how long it takes them to cough up some JavaScript to do it (without cheating ;-) ).

Enter a positive integer up 999 quintillion

Open Unformatted Code In New Window

<table>
<tr><td><strong>Enter a positive integer up 999 quintillion</strong></td></tr>
<tr>
<td>
<input id="number" style="width:250px;" type="text" onkeyup="spellNumber()"/>
</td>
</tr>
</table>

<p id="numberSpelled">
</p>

<script type="text/javascript">

/**
 * Spell an integer. Input from <input id="number">.
 * Output to <p id="numberSpelled">
 * @return void
 */
function spellNumber()
{
	var theString = document.getElementById("number").value;
		
	var firstTwenty = 
	// 1st entry blank as zero is special
	[
		"","one","two","three","four","five","six","seven","eight",
		"nine","ten","eleven","twelve","thirteen","fourteen","fifteen",
		"sixteen","seventeen","eighteen","nineteen"
	];
	
	var firstTens =
	// first entries blank as 1-20 are special
	[
		"","","twenty","thirty","forty","fifty","sixty", 
		"seventy", "eighty", "ninety",
	];
	
	var hundred = "hundred";
	
	var thousands = 
	// first blank as 0-999 are special
	[
		"", "thousand","million","billion","trillion",
		"quadrillion","quintillion"
	];
	
	// used as a stack
	var output = new Array();
	
	var length = theString.length;
	var iterations = Math.floor(length / 3);
	
	if(length % 3 > 0) 
	// add an iteration for the remainder
	{
		iterations++;		
	}
	
	var end = length;
	
	for(var i = iterations - 1, thousandsCol = 0; i > -1; i--, thousandsCol++)
	// go through the number 10^3 at a time
	{
		var start = end - 3;
		
		if(start < 0) 
		{
			start = 0;
		}
	
		// base data
		//***************************************
		var next = theString.substring(start, end);
		var num = parseInt(next, 10);
	
		// calculate the columns
		//***************************************
		var hundredCount = Math.floor(num / 100);
		
		var tenCount = (num - (hundredCount * 100));
		
		if(tenCount > 0)
		{
			tenCount = Math.floor(tenCount / 10);
		}
		
		var oneCount = num - ((hundredCount * 100) + (tenCount * 10));
		
		// generate output
		//***************************************
		if(num > 0)
		// output unit for this 10^3 (thousands, millions, billions, etc.)
		{
			output.push(thousands[thousandsCol]);
		}
		
		if(tenCount <= 1) 
		// output less than 20 (teens, etc.)
		{
			output.push(firstTwenty[(tenCount * 10) + oneCount]);
		}
		else
		// more than 20
		{
			output.push(firstTwenty[oneCount]);
			output.push(firstTens[tenCount]);
		}
		
		if(hundredCount > 0)
		// output hundreds
		{
			output.push(hundred);
			output.push(firstTwenty[hundredCount]);
		}
		
		end -= 3;
	}
	
	// format + show output
	//***************************************
	var outputString = "";
	
	while(output.length > 0)
	{
		var next = output.pop();
		
		if(next == null || next.length == 0) 
		{
			continue;
		}
		
		if(outputString.length > 0)
		{
			outputString += " ";
		}
		
		outputString += next;
	}
	
	if(outputString.length == 0)
	{
		outputString = "zero";
	}
	
	document.getElementById("numberSpelled").innerHTML = outputString;	
}

</script>

Bookmark and Share

Leave a Reply