Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

numeric calculation 1

Status
Not open for further replies.

scribbler

MIS
Feb 4, 2002
206
0
0
GB
Can anyone please tell me what I'm doing wrong here.

My page receives a variable from a previous page but when I try to use it in a simple adding function I get an answer but not quite what I expected such as If the value contains a comma eg 2,500.00 then my calculation only sees the 2 and adds the value 2 to my other variable. If I hard code the variable it still returns the same so I know it's not the request.form but my function but I have been unable to find a solution on the web.

Code:
<SCRIPT LANGAUAGE="javaScript">
function calcTotal() {
    var f = document.forms['form1'];
    var BrokerComm = parseFloat(f.elements['txtBroker'].value);
    var CortsTotalWithOptions = parseFloat(f.elements['CortsPriceWithOptionsHidden'].value);
    f.elements['txtCortsTotal'].value = parseFloat(BrokerComm) + parseFloat(CortsTotalWithOptions);
}
</script>
 
Add the following line to your code:

Code:
var f = document.forms['form1'];
[highlight]f.elements['txtBroker'].value = (f.elements['txtBroker'].value).replace(/,/g, ''); //removes commas[/highlight]
var BrokerComm = parseFloat(f.elements['txtBroker'].value);

'hope that helps.

--Dave
 
Thanks for taking the time to reply Dave.

The line that I think is giving me the problem is
Code:
  var CortsTotalWithOptions = parseFloat(f.elements['CortsPriceWithOptionsHidden'].value);

So I added 2 lines using the same theory but still the same problem

My code now appears as follows (just incase I misunderstood you..
Code:
<SCRIPT LANGAUAGE="javaScript">
function calcTotal() {
    var f = document.forms['form1'];
	f.elements['txtBroker'].value = (f.elements['txtBroker'].value).replace(/,/g, ''); //removes commas
    var BrokerComm = parseFloat(f.elements['txtBroker'].value);
	f.elements['CortsPriceWithOptionsHidden'].value = (f.elements['CortsPriceWithOptionsHidden'].value).replace(/,/g, ''); //removes commas
    var CortsTotalWithOptions = parseFloat(f.elements['CortsPriceWithOptionsHidden'].value);
    f.elements['txtCortsTotal'].value = parseFloat(BrokerComm) + parseFloat(CortsTotalWithOptions);
}
</script>
 
Are the values you're removing commas from on the screen? Can you see if the commas are being removed? If they are not on the screen, add some alert(...) statements along the way to see if the values are being changed appropriately.

You shouldn't need to use parseFloat again in the line
Code:
f.elements['txtCortsTotal'].value = parseFloat(BrokerComm) + parseFloat(CortsTotalWithOptions);
since both values have been parseFloat-ed already.

Are your symptoms the same as before? I.e., are you still only getting a 2 where you believe you should be getting 2500.00? ...or is it some other kind of error now?

--Dave
 
Yes the errors appear on screen and my problem remains the same in that whatever value is entered in my textbox, say 100.50, it gets added to the preceeding variable 'CortsTotalWithOptions' which in this case is 12,387.00 but the resulting total is 112.5
 
Can you post a link to this? I think there's a little something more going on here than meets the eye.

--Dave
 
Hi Dave

I was just removing the password protection on the pages you'd need, saved the 2 pages you'd need access to, uploaded them and hey preston it works fine now. I definately uploaded them earlier but maybe the problem page ahdn't been refreshing properly even though I'd tried a few times with various values.

Thank you again for your help in solving my problem. Thats the 2nd time you've come to my aid at this time of night.

Cheers
Colin
 
Ha! I think this is ONE reason why people outsource their help-desks. It's 2:45 PM where I am! Wide awake and kicking! :)

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top