Brother the numbers in javascript need to be written or qouted. You will have to use something like
alert(91.42 + "+" + 40); // Or else use this alert("91.42" + "+" + "40") // this is more better. Because it will write every block as it is. // And if you want to get the output just as 91.42 40 than use alert("91.42" + " " + "40") // you can see I am using a space in the qoutes between..
Never mind these..
Please "Marks As Answer" if any answer helped you out!
~~! FIREWALL !~~
hunglech
0 Points
2 Posts
Problem with adding two numbers in javascript?
Jan 21, 2013 03:45 AM|LINK
When add following two numbers in javascript:
Ex1:
alert(91.23 + 40); the result is 131.230000000000002
===========================
Ex2: 99.83 + 105 the result is 204.8299999..98
I don't know why? Please help me.
Thanks!
Arshad Ashra...
Member
664 Points
158 Posts
Re: Problem with adding two numbers in javascript?
Jan 21, 2013 03:59 AM|LINK
For rounding fixed digits use following rule in javascript
Hope Help This.....
saeed_saedva...
Member
408 Points
74 Posts
Re: Problem with adding two numbers in javascript?
Jan 21, 2013 06:04 AM|LINK
you should use toFixed(x) method for decimal values. here is a good reference:
http://www.w3schools.com/jsref/jsref_tofixed.asp
Saeed Saedvand
ManikandanUl...
Participant
850 Points
253 Posts
Re: Problem with adding two numbers in javascript?
Jan 21, 2013 06:23 AM|LINK
Hi,
var a =10.1
var b = 5.5
var Total=parseFloat(a)+ parseFloat(b);
alert(Total)
Click "…Mark As Answer" if my reply helpful....
hunglech
0 Points
2 Posts
Re: Problem with adding two numbers in javascript?
Jan 21, 2013 08:42 AM|LINK
Thank you all for for your response.
When I try:
var a = 91.23, b = 40;
var c = parseFloat(a) + parseFloat(b);
alert(c);
I think the result is 131.23 but it return 131.230000..02
saeed_saedva...
Member
408 Points
74 Posts
Re: Problem with adding two numbers in javascript?
Jan 21, 2013 10:24 AM|LINK
<head> <meta charset="utf-8" /> <title></title> <script type="text/javascript"> function add() { var a = 91.23, b = 40; alert((a + b).toFixed(2)); } </script> </head> <body onload="add()"> </body>Saeed Saedvand
Afzaal.Ahmad...
Contributor
2759 Points
1060 Posts
Re: Problem with adding two numbers in javascript?
Jan 21, 2013 04:04 PM|LINK
Brother the numbers in javascript need to be written or qouted. You will have to use something like
alert(91.42 + "+" + 40); // Or else use this alert("91.42" + "+" + "40") // this is more better. Because it will write every block as it is. // And if you want to get the output just as 91.42 40 than use alert("91.42" + " " + "40") // you can see I am using a space in the qoutes between..Never mind these..
~~! FIREWALL !~~