Click to See Complete Forum and Search --> : JS to change currency to text


danmcco
February 28th, 2003, 12:44 PM
The script below almost works : )

for $999,999,999.99 it gives
Nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine dollars and ninety-nine cents

but for $900,990,100.00 it gives
Nine hundred nine hundred ninety thousand one hundred dollars

I have been looking at this too long. Help!

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
function filterNum(str) {
re = /^\$|,/g;
// remove "$" and ","
return str.replace(re, "");
}
// End -->

</script><script language="JavaScript">
<!--
function makeArray0() {
for (i = 0; i<makeArray0.arguments.length; i++)
this[i] = makeArray0.arguments[i];
}

var numbers = new makeArray0('','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve'
,'thirteen','fourteen','fifthteen','sixteen','seventeen','eighteen','nineteen');

var numbers10 = new makeArray0('','ten','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety');

var numbers100 = new makeArray0('','one hundred','two hundred','three hundred','four hundred','five hundred','six hundred','seven hundred','eight hundred','nine hundred');


function chequeAmount(input) {

var dollars = Math.floor(input);
var cents = Math.round((input*100 - dollars*100));
var cmillions = (dollars - dollars % 100000000) / 100000000;
dollars -= cmillions * 100000000;
var millions = (dollars - dollars % 1000000) / 1000000;
dollars -= millions * 1000000;
var cthousands = (dollars - dollars % 100000) / 100000;
dollars -= cthousands * 100000;
var thousands = (dollars - dollars % 1000) / 1000;
dollars -= thousands * 1000;
var hundreds = (dollars - dollars % 100) / 100;
dollars -= hundreds * 100;

var output = '';

output += (cmillions > 0 ? fN(cmillions) + ' hundred ' : '') +
(millions > 0 ? fN(millions) + ' million ' : '') +
(cthousands > 0 ? fN(cthousands) + ' hundred ' : '') +
(thousands > 0 ? fN(thousands) + ' thousand ' : '') +
(hundreds > 0 ? fN(hundreds) + ' hundred ' : '') +
(dollars > 0 ? fN(dollars) + ' ' : '') +
((millions > 0 || cthousands > 0 || thousands > 0 || hundreds > 0 || dollars > 0) ? 'dollars ' : '') +
((Math.floor(input) > 0 && cents > 0) ? 'and ' : '') +
(cents > 0 ? fN(cents) + ' cents' : '');

return output.substring(0,1).toUpperCase() + output.substring(1);
}

function fN(i) {
if (i<20) return numbers[i];
var cs = (i - i % 10) / 100, tens = (i - i % 10) / 10, units = i - (i - i % 10);
return numbers10[tens] + ((cs > 0 && tens > 0 && units > 0) ? '-' : '') + numbers[units];
}
//--></script>

</head>

<body>


<form>
<input type="text" name="amount" size="20">
<input type="text" name="answer" size="70">
<input type="button" value="Strip $ & ," onClick="this.form.answer.value=filterNum(this.form.amount.value)">
<input type="button" value="Convert" onClick="this.form.answer.value=chequeAmount(this.form.answer.value)">
</form>

</body>

</html>


__________________
Dan

antares686
February 28th, 2003, 03:44 PM
This is a common mistake and I made it the first time I wrote the same thing for T-SQL in SQL Server.

What it is is you are accounting for n or nn in the millions and thousands position to account for the word value but for the n hundreds position you are not.

This will fix you up thou.

Change


output += (cmillions > 0 ? fN(cmillions) + ' hundred ' : '') +
(millions > 0 ? fN(millions) + ' million ' : '') +
(cthousands > 0 ? fN(cthousands) + ' hundred ' : '') +
(thousands > 0 ? fN(thousands) + ' thousand ' : '') +
(hundreds > 0 ? fN(hundreds) + ' hundred ' : '') +
(dollars > 0 ? fN(dollars) + ' ' : '') +
((millions > 0 || cthousands > 0 || thousands > 0 || hundreds > 0 || dollars > 0) ? 'dollars ' : '') +
((Math.floor(input) > 0 && cents > 0) ? 'and ' : '') +
(cents > 0 ? fN(cents) + ' cents' : '');


to


output += (cmillions > 0 ? fN(cmillions) + ' hundred ' : '') +
(millions > 0 ? fN(millions) : '') +
((cmillions > 0 || millions > 0) ? ' million ' : '') +
(cthousands > 0 ? fN(cthousands) + ' hundred ' : '') +
(thousands > 0 ? fN(thousands) : '') +
((cthousands > 0 || thousands > 0) ? ' thousand ' : '') +
(hundreds > 0 ? fN(hundreds) + ' hundred ' : '') +
(dollars > 0 ? fN(dollars) + ' ' : '') +
((cmillions > 0 || millions > 0 || cthousands > 0 || thousands > 0 || hundreds > 0 || dollars > 0) ? 'dollars ' : '') +
((Math.floor(input) > 0 && cents > 0) ? 'and ' : '') +
(cents > 0 ? fN(cents) + ' cents' : '');


WHat was however kind of ironic is you took them into account for 'dollors' to be output. You did miss cmillion there thou so 900,000,000 would have output 'Nine Hundred' originally.

Hope this helps.