When I try to compare two numbers using JavaScript Number()
function, it returns false
value for equal numbers. However, the grate
If you are using floating numbers and if they are computed ones. Below will be a slightly more reliable way.
console.log(Number(0.1 + 0.2) == Number(0.3)); // This will return false.
To reliably/almost reliably do this you can use something like this.
const areTheNumbersAlmostEqual = (num1, num2) => {
return Math.abs( num1 - num2 ) < Number.EPSILON;
}
console.log(areTheNumbersAlmostEqual(0.1 + 0.2, 0.3));