我发现钛中的浮点数有一个非常有线的问题。由于某些模块必须输入浮点数,所以必须使用浮点数解决方案,请不要用字符串方法回答我。
下面是问题:当我尝试 Ti.API.info(94.1) 时,它显示了 94.0999999....警报(94.1),它显示 94.1
因此,我在下面做了一点尝试:
for (var i = 0; i<1000; i++)
Ti.API.info(i+0.1);
for (var i = 0; i<1000; i++)
Ti.API.info(i+0.2);
for (var i = 0; i<1000; i++)
Ti.API.info(i+0.3);
for (var i = 0; i<1000; i++)
Ti.API.info(i+0.4);
....
以下是 Titanium 中不寻常的输出(但在浏览器中很常见,例如 64.1)
[INFO] : 64.09999999999999
[INFO] : 65.09999999999999
[INFO] : 66.09999999999999
[INFO] : 67.09999999999999
[INFO] : 68.09999999999999
[INFO] : 69.09999999999999
...
[INFO] : 70.09999999999999
[INFO] : 71.09999999999999
[INFO] : 72.09999999999999
[INFO] : 73.09999999999999
[INFO] : 74.09999999999999
[INFO] : 75.09999999999999
[INFO] : 76.09999999999999
[INFO] : 77.09999999999999
[INFO] : 78.09999999999999
[INFO] : 79.09999999999999
[INFO] : 80.09999999999999
[INFO] : 81.09999999999999
[INFO] : 82.09999999999999
[INFO] : 83.09999999999999
[INFO] : 84.09999999999999
[INFO] : 85.09999999999999
[INFO] : 86.09999999999999
[INFO] : 87.09999999999999
[INFO] : 88.09999999999999
[INFO] : 89.09999999999999
[INFO] : 90.09999999999999
[INFO] : 91.09999999999999
[INFO] : 92.09999999999999
[INFO] : 93.09999999999999
[INFO] : 94.09999999999999
[INFO] : 95.09999999999999
[INFO] : 96.09999999999999
[INFO] : 97.09999999999999
[INFO] : 98.09999999999999
[INFO] : 99.09999999999999
...
[INFO] : 8.199999999999999
[INFO] : 9.199999999999999
...
[INFO] : 8.300000000000001
[INFO] : 9.300000000000001
...
[INFO] : 64.40000000000001
[INFO] : 65.40000000000001
[INFO] : 66.40000000000001
[INFO] : 67.40000000000001
[INFO] : 68.40000000000001
[INFO] : 69.40000000000001
[INFO] : 70.40000000000001
[INFO] : 71.40000000000001
[INFO] : 72.40000000000001
[INFO] : 73.40000000000001
[INFO] : 74.40000000000001
[INFO] : 75.40000000000001
[INFO] : 76.40000000000001
[INFO] : 77.40000000000001
[INFO] : 78.40000000000001
[INFO] : 79.40000000000001
[INFO] : 80.40000000000001
[INFO] : 81.40000000000001
[INFO] : 82.40000000000001
[INFO] : 83.40000000000001
[INFO] : 84.40000000000001
[INFO] : 85.40000000000001
[INFO] : 86.40000000000001
[INFO] : 87.40000000000001
[INFO] : 88.40000000000001
[INFO] : 89.40000000000001
[INFO] : 90.40000000000001
[INFO] : 91.40000000000001
[INFO] : 92.40000000000001
[INFO] : 93.40000000000001
[INFO] : 94.40000000000001
[INFO] : 95.40000000000001
[INFO] : 96.40000000000001
[INFO] : 97.40000000000001
[INFO] : 98.40000000000001
[INFO] : 99.40000000000001
...
我也尝试过类似 parseFloat(x.toFixed(1))、Math.round(x*10)/10 .....等等,它的输出是一样的。帮助