470,648 Members | 1,587 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,648 developers. It's quick & easy.

The sum of two input fields

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
5 7621
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
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
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
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
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.

Similar topics

4 posts views Thread by TJ | last post: by
1 post views Thread by Oeln | last post: by
2 posts views Thread by Rosebud | last post: by
3 posts views Thread by ShadowMan | last post: by
9 posts views Thread by Mike Minor | last post: by
1 post views Thread by Peter Kirk | last post: by
5 posts views Thread by Bart van Deenen | last post: by
3 posts views Thread by Adam Smith | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.