This Message Forum is no longer in use

Please use the new Bravenet Help Forums FOUND HERE

Forum: General Forum
This forum is locked and posting is not allowed
Author
Comment
add money amounts

My function adds the amounts of items checked on my purchase page and I send my assigned variables to the merchant processing site. All items having dollar-only amounts process correctly, but items which have dollars and cents will not add correctly. I've set the default to 0.00 thinking that identifies the number as a decimal, I've multiplied the amount by 1 or 1.00. The result of the addition of 12.95 + 42.95 would be 55.9000000006 and that is not accepted by the merchant code.

In my programming experience the fields must be identified as text or numberic, but I can't find out how to do this in my html code.

Thanks for any help,
Ruth

Browser: IE

OS: Windows 7

Re: add money amounts

HTML is purely a text formatting language, the concept of fields does not exist. The sort of processing that you are attempting needs a scripting language such as Javascript or PHP for validation and calculation. Any rounding must be explicitly coded by you.

Back in the days when you couldn't buy off the shelf programs the usual way to handle money was to work in the minor unit (cents, pence etc) and just drop the decimal point in the right place when it came to display.

Re: add money amounts

Sorry for being unclear. This happens in a javascript function embedded in my html.

Does that help?

Browser: IE

OS: Windows 7

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