When I call this function f = new Function("return 2.3*3;")
I get 6.8999999999999995 instead of 6.9. How to have the exact result?
Asked
Active
Viewed 1,700 times
-1

asv
- 3,332
- 7
- 24
- 47
-
May I ask why you are using `eval`? – Olian04 Jan 20 '18 at 21:03
-
`eval`... Yuck! – Marventus Jan 20 '18 at 21:05
-
I changed with Function – asv Jan 20 '18 at 21:13
1 Answers
2
Use toFixed function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
var num = eval("2.3*3")
console.log(num.toFixed(1))

RICKY KUMAR
- 643
- 4
- 11