PDA

View Full Version : Convert Numbers To Text



MJH
09-22-2009, 05:06 PM
1) CODE TITLE: Convert Numbers to Text

2) AUTHOR NAME/NOTES: Michael J. Hill www.javascript-demos.com

3) DESCRIPTION: For example, the code converts 1865, to One Thousand Eight Hundred Sixty Five. The range is -999,999,999 to 999,999,999. No range test is included, because you may want a different range than the maximum, and you may not want to support negative numbers. Commas, dollar signs, etc, are optional, and/or ignored.

or, ATTACHED BELOW (see #3 in guidelines below):


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>None</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

function toText(currSegment,numberWords,nPlaceHolder){

var wordStr = "";
var nHundred = "";
var nTen = "";
var nUnit = "";
nUnit = numberWords['unitWords'][nPlaceHolder[currSegment].charAt(0)-1];
nTen = numberWords['tensWords'][nPlaceHolder[currSegment].charAt(1)-1];
nHundred = numberWords['unitWords'][nPlaceHolder[currSegment].charAt(2)-1];
nHundred = nHundred != undefined && nHundred != 0 ? nHundred += " Hundred " : "";
nUnit = nUnit != undefined && nUnit != 0 ? nUnit : "";
if (nTen != "Ten")
{
nTen = nTen == 0 || nTen == undefined ? "" : nTen + " ";
}
else {
if (nUnit != "")
{
nTen = numberWords['tenPlusWords'][nPlaceHolder[currSegment].charAt(0)-1];
nUnit = "";
}
}
return wordStr = nHundred + nTen + nUnit;
}

function convertNumeric(nForm){

var wordStr = "";
var numberWords = [];
numberWords['unitWords'] = ["One","Two","Three","Four","Five","Six","Seven","Eight","Nine"];
numberWords['tensWords'] = ["Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"];
numberWords['tenPlusWords'] = ["Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen",
"Seventeen","Eighteen","Nineteen"];
numberWords['placeHolderWords'] = ["hundreds","hundred-thousands","millions"];
var nNumeric = nForm['numeric'].value;
nNumeric = nNumeric.replace(/[^-,0-9]/g, "");
var nNegative = nNumeric.charAt(0) == "-" ? true : false;
nNumeric = nNumeric.replace(/[^0-9]/g, "");
if (nNumeric == 0)
{
nForm['txtResult'].value = "Zero";
return;
}
while(/(\d+)(\d{3})/.test(nNumeric))
{
nNumeric = nNumeric.replace(/(\d+)(\d{3})/,"$1|$2")
}
nNumeric = nNumeric.replace(/^(\d)$/, "00$1");
nNumeric = nNumeric.split("|");
for (each in nNumeric)
{
nNumeric[each] = nNumeric[each].split("").reverse().join("");
}
nNumeric.reverse();
var nPlaceHolder = {};
for (each in numberWords['placeHolderWords'])
{
nPlaceHolder[numberWords['placeHolderWords'][each]] = 0;
}
for (i=0; i<nNumeric.length; i++)
{
nPlaceHolder[numberWords['placeHolderWords'][i]] = nNumeric[i]
}
if (nPlaceHolder['millions'] != 0)
{
wordStr = toText("millions",numberWords,nPlaceHolder) + " Million ";
}
if (nPlaceHolder['hundred-thousands'] != 0)
{
wordStr += toText("hundred-thousands",numberWords,nPlaceHolder) + " Thousand ";
}
if (nPlaceHolder['hundreds'] != 0)
{
wordStr += toText("hundreds",numberWords,nPlaceHolder);
}
wordStr = nNegative ? "Negative " + wordStr : wordStr;
nForm['txtResult'].value = wordStr.replace(/\s{2,}/g, " ");
}

function init(){

var nForm = document.forms[0];
nForm['convertBtn'].onclick = function()
{
convertNumeric(this.form);
}
}

navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);

</script>
<style type="text/css">

label {font-family: tahom; font-size: 11pt;}
.data {padding-left: 5px; font-family: veranda; font-size: 10pt}

</style>
</head>
<body>
<form action="" method="post">

<label>Text Result: <input type="text" name="txtResult" size="120" class="data" readonly>

<br><br>

<label>Numeric (max: -999,999,9999 to 999,999,999 commas optional): <input type="text" name="numeric" size="15" class="data"></label>

<br><br>

<input type="button" name="convertBtn" value="Convert">


</form>
</body>
</html>