This Message Forum is no longer in use

Please use the new Bravenet Help Forums FOUND HERE

General Forum
This Forum is Locked
Author
Comment
View Entire Thread
Re: add money amounts

JavaScript has a floating point inaccuracy. Since you cannot round the number perhaps it would be wise to find a way to work in pence, as suggested in the last post, or at least work with pounds and pence separately.

Browser: Mozilla Firefox 3.6.*

OS: Windows 7

Re: add money amounts

Thanks for your help.

Browser: IE

OS: Windows 7

Re: add money amounts

Why can't you round it off? Truncation might cause issues, but rounding shouldn't cause any problems.

When I first started writing in Javascript I realized that, the first thing I needed was a group of functions to handle numeric operations. So I created a lot of conversion/rounding functions. One of the many functions was to handle what happens on the right of the decimal point. One of my functions rounds a number to almost any degree of accuracy by just specifying the number of decimal points in the function call.

Currency poses a little different issue, because you always want 2 decimal digits, even if both of them are zero. Often, for display purposes only, you require a dollar sign, pound sign, and commas at the thousand marks. But if you just want the number with 2 decimal digits, you can simplify things.

First, start by deleting all of those multiplications by one. They don't do anything for the number. I know your intention is to be sure they are numbers, but that isn't how you do it. In Javascript, when you want to be sure the value is a number (float value), you use the "parseFloat()" function. This is very useful when you are extracting a "value" from a form element. Form element "values" are generally strings, even if they only contain numbers, and need to be converted to a float value.

In your case, some of the form element values are written as float values and others as strings. Inconsistency leads to lots of problems. Because of that, the equation where you calculate the "subtotal" should look like the following code.

Code:
subtotal = parseFloat(form.vol1.value) + parseFloat(form.vol2.value) + parseFloat(form.vol3.value) +
parseFloat(form.vol5.value) + parseFloat(form.vol6.value) + parseFloat(form.vol7.value) +
parseFloat(form.vol8.value) + parseFloat(form.vol9.value) + parseFloat(form.vol10.value) +
parseFloat(form.vol11.value) + parseFloat(form.vol12.value) + parseFloat(form.vol13.value) +
parseFloat(form.vol14.value) + parseFloat(form.vol15.value) + parseFloat(form.legalguidelines.value) +
parseFloat(form.vaccinefree.value) + parseFloat((form.rad[Count].value * issues));


This insures that "subtotal" is a float value when you try to put it into the "transactionamount" form value. But when you do that, you want to call a function that takes care of the formatting.

Code:
form.transactionamount.value = formatCurrency( subtotal + shippingrate );


The function to perform the formatting is listed below. It's modified from something I found on "javascriptsource.com". Mostly I pulled out the parts that added/deleted a "$" and commas at the thousand marks. I know it says "cents" in the function, but you can change the variable to say "pence" if you like.

Code:
function formatCurrency(num) {
if(isNaN(num))
num = "0";
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
return (num + '.' + cents);
}


I have tested this all out in a hacked up version of your "purchase" page. I got rid of the alerts and made the transaction form elements visible. Then I added some "on click" operations to the check boxes, so I could see it in operation. I can make it available to you, if you like. Then some of this might be a little clearer.

Re: add money amounts

You're a lifesaver! Yes, if you still have that webpage code, I'd love to have it!

Thanks for your help,

Ruth

Browser: IE

OS: Windows 7

Re: add money amounts

Ruth,

OK, here is a link to the code that I hacked up - backEditions.html. Just click on the first couple of check boxes and watch the number add up in the "transactionamount" and "orderstring" text boxes.

And here is a link to a ZIP file that contains everything for the hacked up page - Homeopaths.zip. Just create a folder and UnZip it into the folder.

Re: add money amounts

Thanks again,
Ruth

PS I bookmarked your website.

Browser: IE

OS: Windows 7

Re: add money amounts

http://www.vemaybayvn.com
http://www.vemaybayvn.com
http://www.vemaybayvn.com

Re: add money amounts

http://www.vemaybayvn.com
http://www.vemaybayvn.com