4.2、Math数学对象(floor、random、sqrt、pow、abs)

简介: 4.2、Math数学对象(floor、random、sqrt、pow、abs)

1、Math数学对象


Math数学对象 作用
Math.floor() 向下取整
Math.random() 0-1的随机数
Math.sqrt(9) 开方
Math.pow(2,4) 乘方
Math.abs() 绝对值


2、Math.floor() 方法(向下取整 )

    // 1、Math.floor(向下取整)
    var number = 6.78;
    var result = Math.floor(number);    //  输出结果:6
    console.log(result);
    var number = -6.78;
    var result = Math.floor(number);    //  输出结果:-7
    console.log(result);

3、Math.random()方法,取随机数(0-1,1-10,1-100)


    // 2、Math.random(),随机数0-1
    var result = Math.random();   //  输出结果:0-1
    console.log(result);
    var result = Math.floor(Math.random() * 10 + 1);    //  输出结果:1-10
    console.log(result);
    var result = Math.floor(Math.random() * 100 + 1);   //  输出结果:1-100
    console.log(result);


4、Math.abs()方法,取绝对值


// 3、Math.abs(),绝对值
    var number = -34.5;
    var result = Math.abs(number);    //  输出结果:34.5
    console.log(result);


5、Math.sqrt()方法,开平方


// 4、Math.sqrt(),开平方
    var number = 16;
    var result = Math.sqrt(number);   //  输出结果:4
    console.log(result);


6、Math.pow()方法,计算乘方


// 5、Math.pow(),计算乘方
    var number = 2;
    var result = Math.pow(number, 3);   //  输出结果:8
    console.log(result);


7、(floor,random)方法随机抽取数组元素

    // 6、(floor,random)随机抽取数组元素,实例:抽奖活动
    var list = ['jasmine', 'qiqi', 'jasmine_qiqi'];
    var index = Math.floor(Math.random() * list.length);
    console.log(list[index]);   //  输出结果:list数组中的一个元素


相关文章
|
3月前
使用 pow() 函数
【10月更文挑战第23天】使用 pow() 函数。
34 2
|
7月前
|
监控 安全 测试技术
BIFFI 494POW5034 with 494POW5033 模块
Biffi 拥有60多年的经验,在可靠、安全的执行和控制解决方案方面处于领先地位,可满足石油和天然气、电力、加工和水行业对阀门自动化的苛刻要求。我们专注于先进的技术,实现了用于控制和监测的智能系统,提供标准和定制解决方案,以最大限度地提高工厂生产率、减少停机时间,并确保在安全环境下的高性能。产品均为顶级产品,在意大利设计和制造,经过严格的可靠性和耐用性测试。Biffi&#39 的生命周期服务计划可确保工厂安全、稳定、经济地运行,提高资产可靠性。我们的全球支持网络可提供快速、专业的协助,包括通过 Biffi 执行学院进行培训。依靠我们的SIL级产品和智能控制装置,可在极端条件下实现最佳性能。
|
Serverless C++
C++中的 sqrt、sqrtl 和 sqrtf
C++库中有多种函数可用于计算数字的平方根。最突出的是使用 sqrt。它以双重作为论据。 header 定义了另外两个内置函数,用于计算一个数字(sqrt 除外)的平方根,该数字的参数类型为float和long double。因此,用于计算C++平方根的所有函数都是:
574 0
Math.pow()
Math.pow()
88 0
|
JavaScript 前端开发
Math.random();
Math.random();
106 0
Math.ceil()
Math.ceil()
171 0
|
安全 iOS开发
iOS开发-math.h/ceil/floor/round
https://blog.csdn.net/acmicpc123/article/details/50280097
159 0
iOS开发-math.h/ceil/floor/round
|
Python
Python浮点数转整数int、round、ceil、floor
Python浮点数转整数int、round、ceil、floor
384 0