How to use the Math method in js
Most people do not understand the knowledge points of this article "how to use the Math method in js", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the Math method in js" article.
1.min () and max () methods
Math.min () is used to determine the minimum value in a set of values. Math.max () is used to determine the maximum value in a set of values.
Alert (Math.min (2, 4, 3, 3, 6, 3, 3, 8, 0, 0, 1)); / / minimum alert (Math.max, 4, 7, 8, 3, 1, 9, 6, 0, 3, 2)); / / maximum
two。 Rounding method
Math.ceil () performs up rounding, that is, it always rounds the value up to the nearest integer
Math.floor () performs down rounding, that is, it always rounds the value down to the nearest integer
Math.round () performs standard rounding, that is, it always rounds the value to the nearest integer
For example:
Alert (Math.ceil); / 26alert (Math.floor); / 25alert (Math.floor); / 25alert (Math.floor); / / 25alert (Math.round); / 26alert (Math.round); / / 26alert (Math.round) / / 25
3.random () method
The Math.random () method returns a random number between 0 and 1, excluding 0 and 1. If you want to be larger than this range, you can apply the following formula:
Value = Math.floor (Math.random () * Total + first value)
For example:
Alert (Math.floor (Math.random () * 10 + 1)); / / randomly generate any number between 1 and 10
For (var I = 0; I