JS_Math常用的10种方法

本文介绍了JavaScript中Math对象的常用方法,包括random()、round()、ceil()、floor()、pow()、sqrt()、abs()、max()、min()以及PI属性。这些方法在编程中用于处理数值计算,例如生成随机数、四舍五入、取整、求幂和平方根等,是JavaScript数值操作的基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  Math常用方法

        1、random()

        ==>语法:Math.random()

        ==>返回值:0~1之间的随机小数,包0不包1

        2、round()

        ==>语法:Math.round(数字)

        ==>返回值:四舍五入以后取整数字,只看小数点后一位

        3、ceil()

        ==>语法:Math.ceil(数字)

        ==>返回值:向上取整后的数字

        4、floor()

        ==>语法:Math.floor(数字)

        ==>返回值:向下取整后的数字

        5、pow()

        ==>语法:Math.pow(数字,多少次幂)

        ==>返回值:数字取幂以后的结果

        6、sqrt()

        ==>语法:Math.sqrt(数字)

        ==>返回值:数字的算术平方根

        ==>只能是平方根

        7、abs()

        ==>语法:Math.abs(数字)

        ==>返回值:数字的绝对值

        8、max()

        ==>语法:Math.max(数字1,数字2,数字3,...)

        ==>返回值:若干个数字中的最大值

        ==>注意:传递数组不好使

        9、min()

        ==>语法:Math.min(数字1,数字2,数字3,...)

        ==>返回值:若干个数字中的最小值

        ==>注意:传递数组不好使

        10、PI属性

        ==>语法:Math.PI

        ==>得到:近似于数学TT 的值

        ==>注意:

            1、使用不需要()

            2、两个字母都要大写

       

       

       

        */

      //1、Math.random

      /* var res = Math.random();

      console.log(res); */

      //2、Math.round()

      /* var res = Math.round(10.499);

      console.log(res); //10 */

      //3、Math.ceil()

      /*  var res = Math.ceil(10.1);

      console.log(res); //11

      //4、Math.floor(s)

      var res = Math.floor(10.1);

      console.log(res);//10 */

      //5、Math.pow()

      /*     var res = Math.pow(2, 3);

      console.log(res); //8

      //6、Math.sqrt()

      var res = Math.sqrt(4);

      console.log(res);//2 */

      //7、Math.abs()

      /*  var res = Math.abs(-4);

      console.log(res); //4

      //8、Math.max()

      var res = Math.max(1, 2, 3, 4, 5, 6);

      console.log(res); //6

      //9、Math.min()

      var res = Math.min(1, 2, 3, 4, 5, 6);

      console.log(res);//1 */

      //10、Math.PI

      /*   var res = Math.PI;

      console.log(res);

      //3.141592653589793 */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值