美文网首页
js 数字运算-简单记录下

js 数字运算-简单记录下

作者: 王芊芊 | 来源:发表于2021-03-26 10:57 被阅读0次
    // 1.取整-保留整数部分
    parseInt(3/2)  // 1
    
    // 2.向上取整---有小数就整数部分加1(非四舍五入)
    Math.ceil(3/2)  // 2
    
    // 3.四舍五入
    Math.round(3/2)  // 2
    
    // 4.向下取整---丢弃小数部分
    Math.floor(3/2)  // 1
    
    // 5.取余
    console.log(7%4);  // 3
    
    // 6.幂运算
    // 取底数的指数次方,即指数所指定的底数相乘。它在EcmaScript 2016 中首次引入
    
    5 ** 5 (返回 3125,相当于 5 * 5 * 5 * 5 * 5 。)
    
    // 7.获取 0~1.0 之间的随机数
    Math.random();
    
    // 8.求所有参数中的最大值---Math.max
    console.log(Math.max(1, 2, 56, 78, 99, 0, 46, 23)); // 99
    
    // 9.求一个数的几次方 第一个参数是底数 第二个参数是指数
    console.log(Math.pow(2, 8)); // 256
    
    // NumberObject.toFixed(num)---num规定小数的位数,num默认为0
    toFixed() 方法:可把 Number 四舍五入为指定小数位数的数字
    // 千位符正则
    .replace(/(\d)(?=(\d{3})+\.)/g, '$1,')    // 转为字符串再转换
    1790002.21.toFixed(1).replace(/(\d)(?=(\d{3})+\.)/g, '$1,')    // 1,790,002.2
    

    相关文章

      网友评论

          本文标题:js 数字运算-简单记录下

          本文链接:https://www.haomeiwen.com/subject/zjqjhltx.html