PDA

View Full Version : simple math error where is it going wrong


scroots
01-31-2003, 08:42 PM
i have
s=(((document.f.b.value+document.f.g.value)/document.f.tp.value)*document.f.ss.value);

mapped out it should be with values
((151+131) divided by 1183)time 300
it should equal around 75
my scripts gives me and answer of 38325
can anybody spot it?

scroots

Danne
01-31-2003, 08:52 PM
((151+131) divided by 1183)time 300

is really ((151131) / 1183) * 300

The browser treats 151 and 131 as strings.

Try something like

parseInt(151) + 131

Roy Sinclair
01-31-2003, 08:54 PM
It's performing string concatenations instead of addition.

Try: s=(((number(document.f.b.value)+number(document.f.g.value))/number(document.f.tp.value))*number(document.f.ss.value));

You really only need the number function where you're using the addition/concatenation operator but for consistency's sake it helps to use it with the other fields as well.

scroots
01-31-2003, 09:00 PM
nice suggestions but neithero f them seem to work.

scroots

Danne
01-31-2003, 09:05 PM
Try this than:

((parseInt(151)+parseInt(131)) / 1183) * 300

or parse all, but it shouldn't be necessary since the problem is in 151 + 131...

scroots
01-31-2003, 09:06 PM
danne your suggestion worked.
thanks to everyone for trying
scroots