[Javascript] How to deal with floating number

What's your expect of the output?:

console.log(0.1 + 0.2 === 0.3);

The answer is 'false'.

Because:

0.1+ 0.2 // 0.30000000000000004

Therefore if we do calculation on Frontend:

const price = 9.33;
const quantity = 3;
console.log(price * quantity); // 27.990000000000002

The result is not correct.

The solution for this:

const anotherPrice = 9.33 * 100;
const anotherQuantity = 3;
console.log((anotherPrice * anotherQuantity) / 100); // 27.99
原文地址:https://www.cnblogs.com/Answer1215/p/11047804.html