前端第二周,Javascript--Math对象

Math 对象 --数学对象:

        //是js的一个内置对象

        //提供了一些可以操作 数字 的 属性 和 方法

        1. Math.PI       兀 :圆周率

        console.log(Math.PI);//3.141592653589793

        2. Math.ceil()    向上取整

        var a1 = Math.ceil(34.4198);

        console.log(a1);//35

        var b1 = Math.ceil(8.001);

        console.log(b1);//9

        var c1 = Math.ceil('34.129');

        console.log(c1);//35

        var d1 = Math.ceil('hello');

        console.log(d1);//NaN  首先要把'hello'转换成number类型,在转换的过程中他不是一个数字,所以就不能转换成数字

        3. Math.floor()    向下取整

        var a2 = Math.floor(34.98);

        console.log(a2);//34

        var b2 = Math.floor(8.001);

        console.log(b2);//8

        var c3 = Math.floor('34.129');

        console.log(c3);///34

        var d4 = Math.floor('hello');

        console.log(d4);//NaN

        4. Math.round()     四舍五入取整

        var a3 = Math.round(34.67);

        console.log(a3);//35

        var b3 = Math.round(34.48);

        console.log(b3);//34

        5.Math.min()       取最小值

        var a4 = Math.min(34, 90, 65, 12, 44);

        console.assert(a4);//12

        var b4 = Math.min(29, 56);

        console.log(b4);//29

        6.Math.max()        最大值

        var a5 = Math.max(34, 90, 65, 12, 44);

        console.log(a5);//90

        var b5 = Math.max(29, 56, 34);

        console.log(b5);//56

        7.Math.abs()       取绝对值

        var a6 = Math.abs(-78);

        console.log(a6);//78

        var b6 = Math.abs(8)

        console.log(b6);//8

        8.Math.pow(num,n)     计算幂次方,计算num的n次方

        var a7 = Math.pow(3, 4);

        console.log(a7);//81

        var b7 = Math.pow(2, 10);

        console.log(b7); //1024

        9.Math.sprt()       计算平方根,开根号

        var a8 = Math.sqrt(9);

        console.log(a8); //3

        var b8 = Math.sqrt(25);

        console.log(b8);//5

        var c8 = Math.sqrt(11);

        console.log(c8); // 3.3166247903554

10. Math.random()  不写  随机取数,返回0-1之间的随机数字

        var n = Math.random();

        console.log(n);//随机生成的0-1之间的数字

        //获取指定区间的随机数字

        //公式:Match.floor(Math.random()*(max-min+1)-min)

        //(max-min+1)加一是为了更好的向下取整

        var n5020 = Math.floor(Math.random() * (50 - 20 + 1) + 20);

        console.log(n5020);

        //展开式子:

        console.log(0.000001);

        console.log(0.000001 * (50 - 20));

        console.log(0.000001 * (50 - 20 + 1));//0.000031

        console.log(0.000001 * (50 - 20 + 1) + 20);//0.000031+20=20.000031

        console.log(Math.floor(0.000001 * (50 - 20 + 1) + 20));//20

        console.log(0.999999);

        console.log(0.999999 * (50 - 20));

        console.log(0.999999 * (50 - 20 + 1));

        console.log(0.999999 * (50 - 20 + 1) + 20);

        console.log(Math.floor(0.999999 * (50 - 20 + 1) + 20));//50

  //封装函数获取指定区间的随机数字

        function randomNum(n1, n2) {

            return Math.floor(Math.random() * (n2 - n1 + 1) + n1);

        }

        var nnn = randomNum(10, 60);

        console.log(nnn);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值