要解析带两个小数位的float,请使用toFixed(2)的概念。以下是代码-
var price = 178932.89;
var tax = 7.9;
var totalPrice = price*tax;
console.log("The actual value="+totalPrice);
var gettingTwoPlacedValues = parseFloat(totalPrice).toFixed(2);
console.log("After getting two placed values="+gettingTwoPlacedValues);要运行以上程序,您需要使用以下命令-
node fileName.js.
输出结果
在这里,我的文件名为demo142.js。这将产生以下输出-
PS C:\Users\Amit\JavaScript-code> node demo142.js The actual value=1413569.8310000002 After getting two placed values=1413569.83