码迷,mamicode.com
首页 > 编程语言 > 详细

[Javascript] How to deal with floating number

时间:2019-06-18 21:38:07      阅读:160      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!