By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,504 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,504 IT Pros & Developers. It's quick & easy.

The sum of two input fields

P: n/a
I cant seem to calculate the sum with:
document.forms[0].boxOne.value + 0.5*document.forms[0].boxTwo.value

If boxOne contains "1", and boxTwo "3" then the line above prints 11.5..
And thats not the sum..

Can anyone help?
Ebbe
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ebbe wrote on 10 mei 2004 in comp.lang.javascript:
I cant seem to calculate the sum with:
document.forms[0].boxOne.value + 0.5*document.forms[0].boxTwo.value

If boxOne contains "1", and boxTwo "3" then the line above prints 11.5..
And thats not the sum..


x = +document.forms[0].boxOne.value + 0.5*document.forms[0].boxTwo.value

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #2

P: n/a
What is happening is that the Javascript is treating your plus symbol as
a concatenation operator rather than an addition one, so it believe it
is being told to contatenate "1" and "1.5" (half of 3) ... i.e. "11.5"

What you need to do is to explicitly state that the two things to be
added are numbers, forcing Javascript to add them rather than
concatenating them.
parseFloat( document.forms[0].boxOne.value ) + parseFloat( 0.5 *
document.forms[0].boxTwo.value )
Fred Basset
fr*********@whosyourdaddy.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3

P: n/a
Fred Basset wrote:
parseFloat( document.forms[0].boxOne.value ) + parseFloat( 0.5 *
document.forms[0].boxTwo.value )


Great! Didn't know that one :)
Ebbe
Jul 23 '05 #4

P: n/a
Lee
Fred Basset said:

What is happening is that the Javascript is treating your plus symbol as
a concatenation operator rather than an addition one, so it believe it
is being told to contatenate "1" and "1.5" (half of 3) ... i.e. "11.5"

What you need to do is to explicitly state that the two things to be
added are numbers, forcing Javascript to add them rather than
concatenating them.
parseFloat( document.forms[0].boxOne.value ) + parseFloat( 0.5 *
document.forms[0].boxTwo.value )


That second term is a bit inefficient.
In order to calculate:

parseFloat( 0.5 * document.forms[0].boxTwo.value )

The engine first converts the element value to a number so
it can be multiplied by 0.5, which yields another number.

Then, because parseFloat requires a string as an argument,
that resulting number has to be converted to a string.

Finally, parseFloat converts it back to a number again.

Jul 23 '05 #5

P: n/a
JRS: In article <40*********************@dread14.news.tele.dk>, seen in
news:comp.lang.javascript, Ebbe <ebbe@skummerDOTcom.?> posted at Mon, 10
May 2004 12:26:19 :
I cant seem to calculate the sum with:
document.forms[0].boxOne.value + 0.5*document.forms[0].boxTwo.value

If boxOne contains "1", and boxTwo "3" then the line above prints 11.5..
And thats not the sum..

Can anyone help?

Anyone who has read the newsgroup FAQ, as posted on Mon & Fri each week,
will be able to do so; try it.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.