+发表新主题
查看: 1657|回复: 0

[代码片段] js Math对象

[复制链接]

[代码片段] js Math对象

[复制链接]
GT雨燕 发表于 2015-12-29 21:22:45 浏览:  1657 回复:  0 [显示全部楼层] |只看大图 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
1. Math:专门封装数学计算常用常量和计算方法的全局对象

         注意:1.Math不能new!所有API都直接用Math.xxx

            2.Math没有构造函数

    何时使用Math:只要进行数学计算

      MathAPI

      1. 取整:

         上取整 : Math.ceil(n)  

        //Math.ceil();上取整

         var n=15.3;

         console.log(Math.ceil(n));

               210959hww486ubyfbrz9w9.png

         下取整: Math.floor(n)

         //Math.floor();下取整

         var n=15.3;

          console.log(Math.floor(n));

             210959hww486ubyfbrz9w9.png

         四舍五入取整:Math.round(n)

           //Math.round();四舍五入取整

           var n=15.5;

           console.log(Math.round(n));

                210959hww486ubyfbrz9w9.png

        Math.round()n.toFixed()比较:

                        参数范围    功能             返回值

           Math.round(n)  任意数字    只能取整         Number

            n.toFixed(d)   0-20     按任意小数位数      String

         自定义round函数:按任意位数四舍五入,返回Number


  • //自定义round函数:按任意位数四舍五入,返回Number
  • function round(num,d){
  •             //将num*=10的d次幂
  • num*=Math.pow(10,d);
  •       num=Math.round(num.toFixed(2));
  • //将num/=10的d次幂
  •               num/=Math.pow(10,d);
  • return num;
  • }
  • console.log(round(28.456,2));
  •         console.log(round(355.56,2));console.log(round(365.56,-1));

复制代码
210959hww486ubyfbrz9w9.png


      2. 乘方、开平方:

          乘方:Math.pow(底数,)

      比如:Math.pow(10,2)

       //乘方

                 var n=10;

               n=Math.pow(n,2);

                 console.log(n);

                210959hww486ubyfbrz9w9.png

          开平方:Math.sqrt(n): n开平方

         var n=100;

                    n=Math.sqrt(n);

                    console.log(n);

                      210959hww486ubyfbrz9w9.png

      3. 获取最大值,最小值:

          Math.max(n1,n2,n3,...) :给定多个数字中的最大值

          Math.min(n1,n2,n3...): 给定多个数字中的最小值

             //获取最大值与最小值

            console.log(Math.max(1,6,5,8,3));

            console.log(Math.min(1,6,5,8,3));

                            210959hww486ubyfbrz9w9.png


            问题: max与min默认不支持数组作为参数!

                 如何让max与min支持从数组中获取最大值?

         解决办法: var maxValue=Math.max.apply(Math,arr);

                var minValue=Math.min.apply(Math,arr);



  • //获取数组的最大值与最小值
  • var arr=[1,3,5,6,8,9,4];
  •         var maxValue=Math.max.apply(Math,arr);
  •         var minValue=Math.min.apply(Math,arr);
  • console.log(maxValue);        console.log(minValue);

复制代码
210959hww486ubyfbrz9w9.png

      


      4. 随机数:Math.random() ;0<=r<1

          任意maxmin之间取随机整数:

            parseInt(Math.random()*(max-min+1)+min);

               Math.floor(Math.random()*(max-min+1)+min);

          比如: 60 ~ 100

                 Math.random()          0<=r<1

                 Math.random()*41      0<=r<41

                 Math.random()*41+60      60<=r<101

                 Math.floor(xxx)         60<=r<=100



  • //随机生成验证码
  • var chars=[];   //62个备选字符
  •     //从48号开始,到57结束,顺序生成0-9十个字符
  • for(var c=48;c<=57;c++){
  •      chars.push(String.fromCharCode(c));
  • }
  • //从65号开始,到90结束,顺序生成A-Z个字符
  • for(var c=65;c<=90;c++){
  •      chars.push(String.fromCharCode(c));
  • }
  • //从97号开始,到122结束,顺序生成a-z个字符
  • for(var c=97;c<=122;c++){
  •      chars.push(String.fromCharCode(c));
  • }
  • function getCode(){
  •      //定义一个数组
  •     var code=[];
  • //当code长度<=4时,反复随机生成4位验证码
  • while(code.length<4){
  • //从0-62中随机生成验证码并压入code中
  •     var i=parseInt(Math.random()*(61-0+1)+0);
  • code.push(chars);
  • }
  • return code.join(""); //xxxx
  • }
  • var code=getCode();
  •     var input="";
  • while((input=prompt("请输入验!!"+code)).toUpperCase()
  •     !=code.toUpperCase()){
  • alert("验证码错误,请重新输入!!");
  • code=getCode();
  • }alert("验证通过!!");

复制代码
210959hww486ubyfbrz9w9.png 210959hww486ubyfbrz9w9.png
回复

使用道具 举报


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版| 赣南网

© 2013-2016 Comsenz Inc. Powered by Discuz! X3.4

用微信扫一扫

赣南网