+ 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
5 Antworten
+ 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.
+ 4
thank you John Wells ....
+ 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....
+ 4
thanks John Wells I got this..
😊😊😊😊
+ 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