Home >Web Front-end >JS Tutorial >Summary and case introduction of mathematical functions in js

Summary and case introduction of mathematical functions in js

不言
不言Original
2018-08-27 10:51:541750browse

This article brings you a summary and case introduction of mathematical functions in js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Math function

Math.random()

Randomly generates decimals from 0 to 1, including 0, excluding 1

<script>
    console.log(Math.random());     // 0.731681187341011
</script>

Math.ceil( )

Returns the smallest integer (ceiling value) greater than the parameter value, rounding up when decimals are encountered

<script>
    console.log(Math.ceil(0.0));    // 0
    console.log(Math.ceil(-0.0));   // -0
    console.log(0 === -0);          // true
    console.log(Math.ceil(2.3));    // 3
    console.log(Math.ceil(-1.5));   // -1
</script>

Math.floor()

Returns the largest integer less than the parameter value (Floor value), round down when encountering decimals

<script>
    console.log(Math.floor(0.0));    // 0
    console.log(Math.floor(-0.0));   // -0
    console.log(0 === -0);           // true
    console.log(Math.floor(2.3));    // 2
    console.log(Math.floor(-1.5));   // -2
</script>

Math.round()

Round to return an integer

<script>
    console.log(Math.round(0.49));      // 0
    console.log(Math.round(-0.49));     // -0  
</script>

Math.max()

Return the maximum value

<script>
    let arr = [0.2, 0.32, 12];
    console.log(Math.max(arr));       // NaN
    console.log(Math.max(0.49, 0.231, 932));      // 932
    console.log(Math.max(0,-0));     // 0  
</script>

Math.min()

Return the minimum value

<script>
    let arr = [0.2, 0.32, 12];
    console.log(Math.min(arr));       // NaN
    console.log(Math.min(0.49, 0.231, 932));      // 0.231
    console.log(Math.min(0,-0));     // -0  
</script>

Case 1

Random integer generation function in any range

<script>
    // 任意范围的随机整数生成函数
    function getIntRadom(min, max){
        let x = Math.random() * (max - min + 1) + min;  // [0,1)*5 + 2 -> [0,5) + 2 -> [2,7)
        return Math.floor(x);                           // [2,7) -> [2,6]         
    }
    console.log(getIntRadom(2,6));
</script>

Case 2

Return the integer part of the value

<script>
    // 返回数值的整数部分
    function getInt(x){
       x = Number(x);
       return x < 0 ? Math.ceil(x) : Math.floor(x);
   }
   console.log(getInt(7.78));      // 7
</script>

Case 3

Return the integer part of the value

<script>
    function random_vlidateCode(x){
        let str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            // str1 += "abcdefghijklmnopqrst";
            str1 += str1.toLowerCase();
            str1 += "0123456789";
        let str = "";
        for(let i = 0; i < x; i++){
            let vlidateCode = Math.floor(Math.random() * str1.length);                
            str += str1.substring(vlidateCode, vlidateCode + 1);                
        }
        return str;
    }
    console.log(random_vlidateCode(4)); // 生成四位随机数
</script>

Related recommendations:

Instructions for using JS math function Exp_Basic knowledge

string.format function code in js_javascript skills

JS Basic Tutorial: Learning javascript anonymous functions

The above is the detailed content of Summary and case introduction of mathematical functions in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn