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

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

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

``````var rounded = 34.923 | 0;
``````

``````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 );
``````

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

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

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

``````<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>
``````

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

``````Math.floor()
``````

``````Math.floor(1+7/8)
``````

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

``````rounded = number - number % 1;
``````

`Math.floor`一样（向`-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);
``````

``````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` .