添加两个整数JavaScript

时间:2017-01-05 10:02:13

标签: javascript

我试图在JavaScript中添加两个数字,但结果不准确。这是我的例子:

var x = 11.12, y = 11.07;



console.log(x + y);  // the result is 22.189999999999998 but the real result is 22.19

对此有何解决方案?

1 个答案:

答案 0 :(得分:0)

这是你需要做的:



var x = 11.12, y = 11.07;
var result = (parseFloat(x) + parseFloat(y)).toFixed(2);
console.log( result );