คำอธิบาย
ใช้ parseFloat() ร่วมกับ toFixed() เพื่อลบตัวเลขทศนิยมได้แม่นยำ หลีกเลี่ยงปัญหาความแม่นยำของ floating point
ตัวอย่างโค้ด
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
ผลลัพธ์
The first Value=4.3 The second Value=3.8 Result is=0.5