[Javascript] How to deal with floating number

2020-12-13 02:40

阅读:633

标签:cal   pre   strong   another   other   result   oat   class   answer   

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

 

[Javascript] How to deal with floating number

标签:cal   pre   strong   another   other   result   oat   class   answer   

原文地址:https://www.cnblogs.com/Answer1215/p/11047804.html


评论


亲,登录后才可以留言!