本文介绍了Firefox和Javascript舍入规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在IE,Opera和Chrome中,我可以得到我所期望的四舍五入数字以5结尾:

  125 toPrecision(2)=> 130 
11.5 toPrecision(2)=> 12

这是我所期望的。



然而,Firefox虽然稍微复杂一些,但却产生了如下的效果:

  125 toPrecision(2)=> 120 // wtf !!! 
11.5 toPrecision(2)=> 12

经过一番搔头之后,我得出结论,Firefox正在使用四舍五入的规则,如果5之前的数字甚至是数字减少,并且5之前的数字是奇数,则数字递增:

  0.5 => 0 
1.5 => 2
2.5 => 2
3.5 => 4等。

我使用四舍五入的结果来测试学生解决方案的伪随机工程问题生成问题输入。在Chrome中输入的问题可能是h = 1020毫米,但在FF,Chrome或Opera中h = 1030毫米。



我需要一个使圆整一致的函数, 0.0001235加起来0.000124,我想要1234到1240,所以我不能用简单的num = Math.floor(num + 0.5);让事情复杂一点,我想输入变量和学生的答案是正确的3 sig挖掘,除非第一个数字是1,在这种情况下,我想4 sig挖:

  234.5 => 235 
134.5 => 134.5

我已经根据第一个数字转换了3或4个信号的解决方案将数字转换为字符串并测试1的第一个非零非小数点和非负数字符 - 不是很漂亮,但它起作用。我可以做类似的四舍五入问题,检查是否要舍入的数字是5,但我想知道是否有一个优雅的按位解决方案。

解决方案

请看这里的测试




I don't know if I'm missing something obvious here but...

In IE, Opera and Chrome, I get what I expect from rounding numbers ending in a 5:

125 toPrecision(2) => 130
11.5 toPrecision(2) => 12

This is what I'd expect.

Firefox, though, is a little more 'sophisticated' yielding the following:

125 toPrecision(2) => 120 //wtf!!!
11.5 toPrecision(2) => 12

After a bit of head scratching, I've come to the conclusion that Firefox is using a 'rounding even' rule where, if the digit before the 5 is even the number rounds down and if the digit before the 5 is odd the number rounds up:

0.5 => 0
1.5 => 2
2.5 => 2
3.5 => 4, etc.

I am using the rounded results to test student solutions to engineering questions with pseudo-randomly generated question inputs. A question input in Chrome could be h=1020 mm but h=1030 mm in FF, Chrome or Opera.

I need a function to make rounding consistent, i.e. I want 0.0001235 to round up to 0.000124 and I want 1234 to round to 1240 so I can't use a simple num = Math.floor(num + 0.5); To complicate matters a bit, I want input variables and student answers to be correct to 3 sig digs unless the first digit is a 1, in which case I want 4 sig digs:

234.5 => 235
134.5 => 134.5

I've hacked a solution to the 3 or 4 sig digs depending upon the first digit by converting the number to a string and testing the first non-zero, non-decimal point and non-negative character for '1' - not pretty, but it works. I could do something similar for the rounding problem, checking whether the digit to be rounded is a 5 but I'm wondering if there is an elegant bit-wise solution.

解决方案

Please take a look at the tests here

http://yuiblog.com/blog/2009/03/10/when-you-cant-count-on-your-numbers/

Also take a look at the SO questions:

https://stackoverflow.com/questions/287744/good-open-source-javascript-math-library-for-floating-point-operations

and

https://stackoverflow.com/questions/744099/javascript-bigdecimal-library/1575569#1575569

这篇关于Firefox和Javascript舍入规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 15:18