Monday, June 11, 2018

JavaScript - Floating Point Miscalculations - toFixed function

The below example demonstrates the usage of toFixed function - which helps in comparison of numbers in case of floating point miscalculations by JS.


var num1 = 2.7;
var num2 = 3.7;

console.log(num1 * num2);

if ((num1 * num2).toFixed(2) == 9.99) {
  console.log('true');
} else {
  console.log('false');
}

Try it out at : http://jsbin.com

No comments:

Post a Comment