要正确减去两个浮点值,请parseFloat()与一起使用toFixed()。
以下是代码-
var firstValue=4.3;
var secondValue=3.8;
console.log("The first Value="+parseFloat(firstValue).toFixed(1)+" The
second Value="+parseFloat(secondValue).toFixed(1))
var result = parseFloat(firstValue).toFixed(1) -
parseFloat(secondValue).toFixed(1);
console.log("Result is="+result);要运行上述程序,您需要使用以下命令-
node fileName.js.
在这里,我的文件名为demo309.js。
输出结果
这将产生以下输出-
PS C:\Users\Amit\javascript-code> node demo309.js The first Value=4.3 The second Value=3.8 Result is=0.5