Click to See Complete Forum and Search --> : maths problem


witold
07-26-2003, 05:03 PM
Hi,

I'm trying to do a simple computation. All values come from form fields. Yet, it seems that I get a string instead of a figure.

---------------
Here's my coding:
//f3 and f4 are names of 2 different forms

var price = f3.m3.value;
var qty = f4.qty.value;

f4.initPrice.value = price * qty;
f4.promoPrice.value = 10;
f4.netPrice.value = f4.initPrice.value-(f4.initPrice.value * f4.promoPrice.value / 100);
f4.tax.value = f4.netPrice.value * 0.22;
f4.grosPrice.value = f4.netPrice.value + f4.tax.value;
--------------
"f4.grosPrice.value" is where the problem occurs

witold

Khalid Ali
07-26-2003, 05:44 PM
where ever you are getting value from the form or propmts etc

enclose the value like this

parseInt(document.formName.fieldName.value)

Charles
07-26-2003, 06:03 PM
"parseInt()" is fine if you are certain that the value is an integer, otherwise you might find that "Number()" or "parseFloat()" is what you need. One note, "parseInt()" and "parseFloat()" start looking at the start of the string and stop when they hit something that isn't part of a number. Given "4 score" they return 4. "Number()" looks at the whole string and given "4 score" it returns "NaN".

witold
07-26-2003, 06:26 PM
Hi,

sorry to say, but none of them works. it must be a coding problem on my side, I guess.

here are some sample figures I used

f4.initPrice.value = 327 * 1;
f4.promoPrice.value = 10;
f4.netPrice.value = 327-(327 * 10/100);
f4.tax.value = 294* 0.22;
//until this moment everything looks ok

f4.grosPrice.value = 294 + 64;
//the result is 29464

strange, ha?

witold

Charles
07-26-2003, 06:57 PM
f4.grosPrice.value = parseInt(294) + (64);

or

f4.grosPrice.value = parseFloat(294) + parseFloat(64);

or

f4.grosPrice.value = Number(294) + Number(64);

witold
07-26-2003, 07:05 PM
Now it's ok. Thanks.

As you can see I'm learning JS the hard way...

:-)