Division Error in java Script? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Division Error in java Script?

in the code attached at the end, why don't we get simply 2.99 as an answer.... What should we do to overcome this problem? please answer in detail... https://code.sololearn.com/WTQgSx9s5dk8/?ref=app

11th Oct 2018, 8:18 PM
Suraj Jha
Suraj Jha - avatar
5 Answers
+ 3
Because 29.9, 10, or the process of dividing does not have an exact binary representation you get the next closest value that does. Since JavaScript does all numbers as doubles, my favorite way to avoid this can't be used. 299/100 does yield 2.99 as expected.
12th Oct 2018, 1:17 AM
John Wells
John Wells - avatar
+ 4
thank you John Wells ....
12th Oct 2018, 3:39 AM
Suraj Jha
Suraj Jha - avatar
+ 4
John Wells will you please suggest me any idea what should we do if we want to perform multiplication... like 0.299*10 = 2.99 and not 2.9899999....
12th Oct 2018, 3:44 AM
Suraj Jha
Suraj Jha - avatar
+ 4
thanks John Wells I got this.. 😊😊😊😊
12th Oct 2018, 4:01 AM
Suraj Jha
Suraj Jha - avatar
+ 3
If you know the number of digits you want, stack overflow suggests multiply, round, divide to get there so: Math.round(29.9*10)/100
12th Oct 2018, 3:47 AM
John Wells
John Wells - avatar