ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

js基础---Math工具类

2021-08-31 14:01:48  阅读:181  来源: 互联网

标签:random js --- 取整 随机数 生成 round Math


 

             * Math.random()
             *     - 可以用来生成一个0-1之间的随机数
             *  - 生成一个0-10的随机数
             *     - 生成一个0-x之间的随机数
             *         Math.round(Math.random()*x)
             * 
             *     - 生成一个1-10
             *     - 生成一个x-y之间的随机数
             *         Math.round(Math.random()*(y-x)+x)

             * Math.ceil()
             *     - 可以对一个数进行向上取整,小数位只有有值就自动进1
             * Math.floor()
             *     - 可以对一个数进行向下取整,小数部分会被舍掉
             * Math.round()
             *     - 可以对一个数进行四舍五入取整
 

 

            /*
             * Math
             *     - Math和其他的对象不同,它不是一个构造函数,
             *         它属于一个工具类不用创建对象,它里边封装了数学运算相关的属性和方法
             *     - 比如
             *         Math.PI 表示的圆周率
             */
            
            //console.log(Math.PI);
            
            /*
             * abs()可以用来计算一个数的绝对值
             */
            //console.log(Math.abs(-1));
            
            /*
             * Math.ceil()
             *     - 可以对一个数进行向上取整,小数位只有有值就自动进1
             * Math.floor()
             *     - 可以对一个数进行向下取整,小数部分会被舍掉
             * Math.round()
             *     - 可以对一个数进行四舍五入取整
             */
            //console.log(Math.ceil(1.1));
            //console.log(Math.floor(1.99));
            //console.log(Math.round(1.4));
            
            /*
             * Math.random()
             *     - 可以用来生成一个0-1之间的随机数
             *  - 生成一个0-10的随机数
             *     - 生成一个0-x之间的随机数
             *         Math.round(Math.random()*x)
             * 
             *     - 生成一个1-10
             *     - 生成一个x-y之间的随机数
             *         Math.round(Math.random()*(y-x)+x)
             */
            /*for(var i=0 ; i<100 ; i++){
                //console.log(Math.round(Math.random()*10));
                //console.log(Math.round(Math.random()*20));
                
                //console.log(Math.round(Math.random()*9)+1);
                //console.log(Math.round(Math.random()*8)+2);
                
                //生成1-6之间的随机数
                console.log(Math.round(Math.random()*5+1));
            }*/
            
            /*
             * max() 可以获取多个数中的最大值
             * min() 可以获取多个数中的最小值
             */
            
            var max = Math.max(10,45,30,100);
            var min = Math.min(10,45,30,100);
            //console.log(min);
            
            /*
             * Math.pow(x,y)
             *     返回x的y次幂
             */
            
            //console.log(Math.pow(12,3));
            
            /*
             * Math.sqrt()
             *  用于对一个数进行开方运算
             */
            console.log(Math.sqrt(2));

 

标签:random,js,---,取整,随机数,生成,round,Math
来源: https://www.cnblogs.com/leiyanting/p/15210009.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有