如何在Javascript中相乘?小数问题 [英] How to multiply in Javascript? problems with decimals
本文介绍了如何在Javascript中相乘?小数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Javascript中使用以下代码:
i've the following code in Javascript:
var m1 = 2232.00;
var percent = (10/100);
var total = percent*m1;
alert(total);
问题是变量total给了我223.20000000000002,它应该是223.2 ,我该怎么做才能得到正确的值?
The problem is that the variable "total" gives me "223.20000000000002" and it should be "223.2", what should i do to get the correct value?
推荐答案
.toFixed()是最佳解决方案。点后只保留两位数。
.toFixed() is best solution.It will keep only two digits after dot.
Exp 1:
var value = 3.666;
value.toFixed(2); //Output : 3.67
Exp 2:
var value = 3.0000;
value.toFixed(2); //Output : 3.00
这篇关于如何在Javascript中相乘?小数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文