请考虑以下代码:
for (var i=0;i<3;i++){
var num = i + 0.50;
var output = num + " " + Math.round(num) + " " + num.toFixed(0);
alert(output);
}
Run Code Online (Sandbox Code Playgroud)
在Opera 9.63中我得到:
0.5 1 0
1.5 2 2
2.5 3 2
在FF 3.03我得到:
0.5 1 1
1.5 2 2
2.5 3 3
在IE 7中我得到:
0.5 1 0
1.5 2 2
2.5 3 3
注意粗体结果.为什么会出现这种不一致的情况?这是否意味着toFixed(0)应该避免?将数字舍入到最接近的整数的正确方法是什么?
代码:
<?php
$start = 0;
$stop = 1;
$step = ($stop - $start)/10;
$i = $start + $step;
while ($i < $stop) {
echo($i . "<br/>");
$i += $step;
}
?>
Run Code Online (Sandbox Code Playgroud)
输出:
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1 <-- notice the 1 printed when it shouldn't
Run Code Online (Sandbox Code Playgroud)
创造了一个小提琴
还有一个:如果设置$start = 1和$stop = 2它工作正常.
使用: php 5.3.27
为什么1印刷?
我想把这两个变量加起来:
xValue 是2.00000000(正面)
yValue 是-0.00001250(负数)
<%= xValue.toFixed(8) + yValue.toFixed(8) %>
Run Code Online (Sandbox Code Playgroud)
输出正在变为: 2.00000000-0.00001250
但我需要看到这个:= 1.9999875
如果我提取变量:
<%= xValue.toFixed(8) - yValue.toFixed(8) %>
Run Code Online (Sandbox Code Playgroud)
没问题:= 2.0000125
我做错了什么?