当前位置: 移动技术网 > IT编程>网页制作>CSS > 较常用的Math方法及ES6中的扩展

较常用的Math方法及ES6中的扩展

2018年12月20日  | 移动技术网IT编程  | 我要评论
记录下与math有关的常用方法,如:求最大值、最小值等,或者是保留几位数啥的 let floata = 2.325232; let floatb = 2.3456; let temporaryarray = [1, 2, 5, 6, 3]; let minusnum = -12; let minusfloat = -12.321; let inta = 10; let min, max, num;

2.求最大值、最小值

{
    /* 求最小值 */
    min = math.min(floata, floatb);
    console.log(min);
    /* 使用apply来重新绑定this */
    min = math.min.apply(null, temporaryarray);
    console.log(min);
    /* 使用展开运算符 */
    min = math.min(...temporaryarray);
    console.log(min);

    /* 求最大值 */
    max = math.max(floata, floatb);
    console.log(max);
    /* 使用apply来重新绑定this */
    max = math.max.apply(null, temporaryarray);
    console.log(max);
    /* 使用展开运算符 */
    max = math.max(...temporaryarray);
    console.log(max);
}

3.取整

{
    /* 四舍五入取整:取与参数最接近的整数 */
    num = math.round(floata);
    console.log(num);

    num = math.round(minusfloat);
    console.log(num);

    /* 向上取整:取大于或等于函数参数,并且与之最接近的整数 */
    num = math.ceil(floatb);
    console.log(num);

    /* 向下取整:取小于或等于函数参数,并且与之最接近的整数 */
    num = math.floor(floatb);
    console.log(num);

    /* ceil、floor结合起来,实现一个总是返回数值的整数部分的函数 */
    function getinteger(value) {
        value = number(value);
        return value < 0  math.ceil(value) : math.floor(value);
    }
    console.log(getinteger(-2.3322));
}

4.求绝对值

{
    /* 负整数 */
    num = math.abs(minusnum);
    console.log(num);

    /* 负浮点数 */
    num = math.abs(minusfloat);
    console.log(num);
}

5.次幂

{
    /* 结果是虚数或负数,则该方法将返回 nan
    * 如果由于指数过大而引起浮点溢出,则该方法将返回 infinity
    */
    /* 2的3次方 */
    num = math.pow(2, 3);
    console.log(num);
}

6.去平方根

{
    /*  求参数的平方根,如果参数小于 0,则返回 nan */
    num = math.sqrt(9);
    console.log(num);
}

7.生成随机数

{
    /* 生成0-1的随机数,大于0小于1 */
    num = math.random();
    console.log(num);

    /* 生成0-10的随机数 */
    num = math.random() * 10;
    console.log(num);

    /* 生成任意范围随机数 */
    function getrandom(min, max) {
        return math.random() * (max - min) + min;
    }
    console.log(getrandom(3.5, 6.5));

    /* 整数min与整数max生成任意范围整数随机数 */
    function getrandomint(min, max) {
        return math.floor(math.random() * (max - min + 1)) + min;
    }
    console.log(getrandomint(5, 15));
}

8.es6中对math方法的扩展(部分)

{
    /* 取整(非四舍五入)
     * 对于非数值,内部调用 number 强转为数值
     * 对于空值或其他数据,返回nan
     * */
    num = math.trunc(floata);
    console.log(num);

    num = math.trunc(inta);
    console.log(num);

    num = math.trunc('aaa');
    console.log(num);
}

{
    /* 判断一个数是正数、负数或零
     * 正数返回+1,负数返回-1,零返回0或-0
     * 其他值 nan
     * */
    num = math.sign(2);
    console.log(num);

    num = math.sign(0);
    console.log(num);

    num = math.sign(-0);
    console.log(num);

    num = math.sign(-14);
    console.log(num);

    num = math.sign('ss');
    console.log(num);
}

{
    let a = 2;
    a **= 3;
    /* 相当于 a*a*a */
    console.log(a);
}

9.保留位数操作

{
    /* 四舍五入保留两位小数
     * tofixed(num) 方法可把 number型 四舍五入为指定小数位数的数字
     * num规定小数的位数,是 0 ~ 20 之间的值,包括 0 和 20
     * 有些实现可以支持更大的数值范围,如果省略了该参数,将用 0 代替
     * */
    num = floata.tofixed(2);
    console.log(num);

    let word = 2.5;
    num = word.tofixed();
    console.log(num);

    /* 不四舍五入 */
    num = math.floor(23.365125 * 100) / 100;
    console.log(num);
}

10.字符串转数字

 {
    /* parseint(value, radix)
    * 用于解析字符串,返回一个整数
    * radix表示要解析的数字的基数,该值介于 2 ~ 36 之间
    * 如果省略该参数或其值为 0,则数字将以 10 为基础来解析
    * 如果它以 “0x” 或 “0x” 开头,将以 16 为基数
    * 如果该参数小于 2 或者大于 36,则 parseint() 将返回 nan。
    * */
    let temporarystring = '123';
    num = parseint(temporarystring);
    console.log(num);

    {
        console.log(parseint("10"));        //返回 10
        console.log(parseint("19",10));        //返回 19 : 10 + 9
        console.log(parseint("11",2));        //返回 3 : 2 + 1
        console.log(parseint("17",8));        //返回 15 : 8 + 7
        console.log(parseint("1f",16));        //返回 31 : 16 + 15
        console.log(parseint("010"));        //未定:返回 10 或 8
    }

    /* 常见的parseint一道题 */
    {
        console.log(['1', '2', '3'].map(parseint));

        /* 返回[1, nan, nan]
        * map(function(value, index, array) {})
        * map方法中的回调函数中的3个参数值,每个值value,索引值index,数组对象array
        * 上面的相当于parseint('1', 0)、parseint('2', 1)、parseint('3', 2)
        * */
    }

    /* number强转 */
    num = number('12345');
    console.log(num);
}
正在努力学习中,若对你的学习有帮助,留下你的印记呗(点个赞咯^_^)

往期好文推荐:

判断ios和android及pc端 纯css实现瀑布流(multi-column多列及flex布局) 微信小程序之购物车和父子传值及calc的注意事项 css实现波浪线及立方体

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网