[Javascript] How to deal with floating number
Posted answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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
以上是关于[Javascript] How to deal with floating number的主要内容,如果未能解决你的问题,请参考以下文章
[错误处理]: How to deal with chrome failing to launch GPU process
sh how-to-deal-command-line-parameters.sh
What is "found.000" ? How to deal with it?
Error: ggplot2 doesn‘t know how to deal with data of class uneval
[Javascript] How to write a Javascript libarary
[] + is equal to [object Object], in Javascript? How? [duplicate]