math js四舍五入保留2位小数 - 我如何在Javascript中舍入数字?





js精度丢失 javascript小数点运算 (9)


Math.floor()可以工作,但与使用按位OR操作相比,它非常慢:

var rounded = 34.923 | 0;
alert( rounded );
//alerts "34"

编辑 Math.floor() 不会比使用|慢 运营商。 感谢Jason S检查我的工作。

以下是我用来测试的代码:

var a = [];
var time = new Date().getTime();
for( i = 0; i < 100000; i++ ) {
    //a.push( Math.random() * 100000  | 0 );
    a.push( Math.floor( Math.random() * 100000 ) );
}
var elapsed = new Date().getTime() - time;
alert( "elapsed time: " + elapsed );

我如何在JavaScript中舍入数字?

math.round()不起作用,因为它会将其math.round()入到最接近的小数点。

我不确定是否有更好的方法去做,而不是在保留第一位时在小数点处分开。 必须有...




向负无穷圆 - Math.floor()

+3.5 => +3.0
-3.5 => -4.0

可以通过Math.ceil()用于负数, Math.floor()用于正数来模拟通向零的圆 - 通常称为Truncate() ,但JavaScript不支持。

+3.5 => +3.0 using Math.floor()
-3.5 => -3.0 using Math.ceil()



您需要将-1减半,然后再乘以-1,就像下面的例子一样。

<script type="text/javascript">

  function roundNumber(number, precision, isDown) {
    var factor = Math.pow(10, precision);
    var tempNumber = number * factor;
    var roundedTempNumber = 0;
    if (isDown) {
      tempNumber = -tempNumber;
      roundedTempNumber = Math.round(tempNumber) * -1;
    } else {
      roundedTempNumber = Math.round(tempNumber);
    }
    return roundedTempNumber / factor;
  }
</script>

<div class="col-sm-12">
  <p>Round number 1.25 down: <script>document.write(roundNumber(1.25, 1, true));</script>
  </p>
  <p>Round number 1.25 up: <script>document.write(roundNumber(1.25, 1, false));</script></p>
</div>



今天围绕着一些elses代码摆弄,发现以下几点似乎也在下降:

var dec = 12.3453465,
int = dec >> 0; // returns 12

有关Sign-propagating右移(>>)的更多信息,请参阅MDN按位运算符

我花了一段时间才弄清楚这是干什么的:D

但正如上面强调的,Math.floor()的工作原理,在我看来看起来更具可读性。




Math.floor()

是答案。




Math.floor(1+7/8)



number舍入为0可以通过减去其带符号的小数部分number % 1

rounded = number - number % 1;

Math.floor一样(向-Infinity四舍五入),此方法非常准确。

虽然在处理-0+Infinity-Infinity存在差异:

Math.floor(-0) => -0
-0 - -0 % 1    => +0

Math.floor(Infinity)    => Infinity
Infinity - Infinity % 1 => NaN

Math.floor(-Infinity)     => -Infinity
-Infinity - -Infinity % 1 => NaN



要向负向无穷大转弯,请使用:

rounded=Math.floor(number);

要向下舍入到零(如果该数字可以舍入到-2147483648和2147483647之间的32位整数),请使用:

rounded=number|0;

要向下舍入为零(对于任何数字),请使用:

if(number>0)rounded=Math.floor(number);else rounded=Math.ceil(number);



You can motivate the solution by thinking about it in terms of symmetries (groups, in math language). No matter the order of the set of numbers, the answer should be the same. If you're going to use k functions to help determine the missing elements, you should be thinking about what functions have that property: symmetric. The function s_1(x) = x_1 + x_2 + ... + x_n is an example of a symmetric function, but there are others of higher degree. In particular, consider the elementary symmetric functions . The elementary symmetric function of degree 2 is s_2(x) = x_1 x_2 + x_1 x_3 + ... + x_1 x_n + x_2 x_3 + ... + x_(n-1) x_n , the sum of all products of two elements. Similarly for the elementary symmetric functions of degree 3 and higher. They are obviously symmetric. Furthermore, it turns out they are the building blocks for all symmetric functions.

You can build the elementary symmetric functions as you go by noting that s_2(x,x_(n+1)) = s_2(x) + s_1(x)(x_(n+1)) . Further thought should convince you that s_3(x,x_(n+1)) = s_3(x) + s_2(x)(x_(n+1)) and so on, so they can be computed in one pass.

How do we tell which items were missing from the array? Think about the polynomial (z-x_1)(z-x_2)...(z-x_n) . It evaluates to 0 if you put in any of the numbers x_i . Expanding the polynomial, you get z^n-s_1(x)z^(n-1)+ ... + (-1)^n s_n . The elementary symmetric functions appear here too, which is really no surprise, since the polynomial should stay the same if we apply any permutation to the roots.

So we can build the polynomial and try to factor it to figure out which numbers are not in the set, as others have mentioned.

Finally, if we are concerned about overflowing memory with large numbers (the nth symmetric polynomial will be of the order 100! ), we can do these calculations mod p where p is a prime bigger than 100. In that case we evaluate the polynomial mod p and find that it again evaluates to 0 when the input is a number in the set, and it evaluates to a non-zero value when the input is a number not in the set. However, as others have pointed out, to get the values out of the polynomial in time that depends on k , not N , we have to factor the polynomial mod p .





javascript math rounding