奇怪的数学计算 [英] weird math calculation

查看:117
本文介绍了奇怪的数学计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Possible Duplicate:
Is JavaScript's Math broken?

我有一个javascript和html代码这里我的代码

I have a javascript and html code here my code

我得到一个奇怪的值当计算特定字段时。只有一个复选框有一个脚本现在(背景音乐插件),并且只有当我选择添加上任何3in卷中的任何一个我得到24.990000000000002在总价格。它是如此奇怪,它只有与这些输入...它应该只是24.99 ....所有其他输入工作,并给出准确的总计,但只是3 3in字段给这些数字...通过试验和错误我发现,问题在这里

and i get a weird value when calculate specific fields. Only one checkbox has a script right now (the background music addon) and only when i select that add on with 1 of any of the 3in reels i get 24.990000000000002 in the total price. it's so weird its only with those inputs...it should be just 24.99....all the other inputs work and give accurate totals but just the 3 3in fields give those numbers...through trial and error I have found that the problem lies here

regthreetot = parseFloat(regthree * 50);
regfourtot = parseFloat(regfour * 100);
regfivetot = parseFloat(regfive * 200);
regsixtot = parseFloat(regsix * 300);
regseventot = parseFloat(regseven * 400);
supthreetot = parseFloat(supthree * 50);
supfourtot = parseFloat(supfour * 100);
supfivetot = parseFloat(supfive * 200);
supsixtot = parseFloat(supsix * 300);
supseventot = parseFloat(supseven * 400);
sixthreetot = parseFloat(sixthree * 50);
sixfourtot = parseFloat(sixfour * 100);
sixfivetot = parseFloat(sixfive * 200);
sixsixtot = parseFloat(sixsix * 300);
sixseventot = parseFloat(sixseven * 400);

regthree,supthree和sixthree值都乘以50 ...所有其他值都相乘由一个3位数的值...如果我改变50到100它会给出一个正常的答案...为什么数字的数量在这里和我能做什么来解决它?

the regthree,supthree and sixthree values are all multiplied by 50...all the other values are multiplied by a value with 3 digits...if i change 50 to 100 it will give a normal answer...why does the number of digits matter here and what can i do to fix it?

推荐答案

它是由浮点变量不是100%准确,修复它到最多2个小数点:

It's caused by float variables not being 100% accurate, to fix it to max 2 decimal points:

n.toFixed(2)

这篇关于奇怪的数学计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆