Click to See Complete Forum and Search --> : another function


fisk
04-12-2003, 07:45 PM
Hello again...I have been trying to figure this one out for awhile. After I enter the check amount I want it to display the equivalent into words. Thank You








<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>
<html>
<head>
<script language = "JavaScript">


function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

</script>
</head>

<body>

<center>
<form name=currencyform>
Enter a number then click the button: <input type=text name=input size=10 value="1000434.23">
<input type=button value="Convert" onclick="this.form.input.value=formatCurrency(this.form.input.value);">


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

viravan
04-12-2003, 10:38 PM
JavaScript still doesn't provide the ability to format a number??? I haven't missed much in the past few years, have I...LOL

:)

V.V.

Nedals
04-12-2003, 11:52 PM
Dave...
You once posted the toFixed() method and I've used it ever since (except on IE5), and it does a great job of formatting currency. Of course, if you want to add ','s then it's a bit more of a problem.

var dollarAmt = "$" + num.toFixed(2);

fisk..
To solve your problem you are going to need some word arrays
var ary1 = new Array('one','two',.... 'nineteen');
var ary2 = new Array('twenty','thirty',....'ninety');
var ary3 = new Array('hundred','thousand',.....);
Now you need to use the digits of your number to get the right word, but before you do that, you have to decide what the words will be

$0.30 -> thirty cents OR no dollars and thirty cents
$5.00 -> five dollars OR five dollars and no cents

It's not an easy script which is probably why Dave does not want to post his. :)

fisk
04-13-2003, 04:07 PM
nedals. Thank you. It looks like it would work but don't know anything about arrays. I don't know where to fit them into the code. Could someone help get me started again? Thanks again.

Nedals
04-13-2003, 07:59 PM
but don't know anything about arrays
fisk, I don't mean to put you off, but the arrays are the easy part of this script.

I would suggest you read though Dave's tutorial, get hold of some javascript documentation, and learn the basics of creating and using arrays. You should then have enough information to get a basic script started. When you get stuck you can post again and someone will help you out.