Page 1 of 1

Adding two floating point values

Posted: Wed Sep 05, 2007 7:59 am
by vinoth
Is there any possible way to add two floating point values in javascript
because if we put + it will concordinate

So anybody know how to add two floating points

I need some suggestions, coding regarding this

Thanks in advance

Posted: Wed Sep 05, 2007 8:03 am
by VladSun
???

Please, post your code...

Posted: Wed Sep 05, 2007 8:11 am
by superdezign
Have you looked at the MDC? Very useful resource to keep handy when working with JavaScript objects.

Posted: Wed Sep 05, 2007 8:14 am
by vinoth
I didn't have any code in general I ask this.

Because the + operator is not used for adding in javascript.

Posted: Wed Sep 05, 2007 8:16 am
by superdezign
vinoth wrote:Because the + operator is not used for adding in javascript.
Since when?

Posted: Thu Sep 06, 2007 4:36 am
by vinoth
I solved the problem by using eval function for adding two float numbers

Posted: Thu Sep 06, 2007 9:49 am
by Begby
The problem is that the floats were actually strings, when you try to add strings it concatenates them.

right
7.4 + 7.5 = 14.9

wrong
'7.4' + '7.5' = '7.47.5'

Posted: Thu Sep 06, 2007 4:59 pm
by feyd
I'd avoid the eval by using the constructors or parseFloat methods instead.