zoukankan      html  css  js  c++  java
  • JS学习笔记10之Math对象

    -->Math对象 常用属性和方法
    -->使用Math对象制作相应的效果

    Math对象用于执行数学任务

    一、Math对象的属性

    二、Math对象的方法

    三、常用属性方法

    Math.PI ----------------返回圆周率3.14 ...
    Math.ceil(x) ------------对数值x进行向上取整
    Math.floor(x) -----------对数值x进行向下取整
    Math.round(x) ----------对数值x进行四舍五入
    Math.min(a,b,c...) -------返回abc...中的最小值
    Math.max(a,b,c...) -------返回abc...中的最大值
    Math.random() --------返回介于0 ~ 1 之间的随机数

     1 <script>
     2 /*Math.PI ----返回圆周率3.14 */
     3     var a=Math.PI;
     4     console.log('a='+a);//a=3.141592653589793
     5 /*Math.ceil(x) ------对数值x进行向上取整*/
     6     console.log('b='+Math.ceil(a));//b=4
     7 /*Math.floor(x) ------对数值x进行向下取整*/
     8     console.log('c='+Math.floor(a));//c=3
     9 /*Math.round(x) -----对数值x进行四舍五入*/
    10     console.log('d='+Math.round(18.500000));//d=19
    11     console.log('d='+Math.round(18.499999));//d=18
    12 /*Math.min(a,b,c...) ----返回abc...中的最小值*/
    13     console.log('min='+Math.min(0,2,5,67,335,63,99));//min=0
    14 /*Math.max(a,b,c...) ---返回abc...中的最大值*/
    15     console.log('max='+Math.max(0,2,5,67,335,63,99));//max=335
    16 /*Math.random() -----返回介于0 ~ 1 之间的随机数*/
    17     console.log('0~1随机数是'+Math.random());//0.22180383793467096
    18 </script>

       四、使用Math对象制作相应的效果 

    1、10个1~20的不重复的随机数

     1 <body>
     2     <h1 id="con1">10个1~20的不重复的随机数</h1>
     3 <script>
     4     var con1=document.getElementById('con1');
     5     var arr=[];
     6     for (var i = 0; i < 10; i++) {
     7         var r=parseInt(Math.random()*20)+1;
     8         arr.push(r);
     9         console.log(arr);
    10         for (var j = 0; j < i; j++) {
    11             if (arr[j]==r) {
    12                 arr.pop();
    13                 i--;
    14                 break;
    15             }
    16         }
    17     }
    18     con1.innerText=arr;
    19     console.log(arr.length);
    20 </script>
    21 </body>
  • 相关阅读:
    CSS截断字符串
    [VB.NET] 打印DataGridView类
    [C#] 打印DataGridView类
    [MySQL] errno:150
    [C#] 用户自定义控件(含源代码)透明文本框
    [分享] 微软面试题
    [C#] 用户自定义控件(含源代码)圆角Panel
    [MySQL] 史上最全的MySQL备份方法
    [C#(WebForm)] 利用递归遍历文件夹和文件存入TreeView
    [VB] (开源)VB高仿Windows7桌面炫丽版
  • 原文地址:https://www.cnblogs.com/paulirish/p/Math1.html
Copyright © 2011-2022 走看看