分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

JS中Math函数的常用方法

发布时间:2023-09-06 02:34责任编辑:董明明关键词:暂无标签

Math 是数学函数,但又属于对象数据类型 typeof Math => ‘object’ 
console.dir(Math) 查看Math的所有函数方法。 
1,Math.abs() 获取绝对值

Math.abs(-12) = 12

2,Math.ceil() and Math.floor() 向上取整和向下取整

 console.log(Math.ceil(12.03));//13 console.log(Math.ceil(12.92));//13 console.log(Math.floor(12.3));//12 console.log(Math.floor(12.9));//12

3,Math.round() 四舍五入 
注意:正数时,包含5是向上取整,负数时包含5是向下取整。

1、Math.round(-16.3) = -122、Math.round(-16.5) = -123、Math.round(-16.51) = -13

4,Math.random() 取[0,1)的随机小数 
案例1:获取[0,10]的随机整数

console.log(parseInt(Math.random()*10));//未包含10
console.log(parseInt(Math.random()*10+1));//包含10

案例2:获取[n,m]之间的随机整数

Math.round(Math.random()*(m-n)+n)

5,Math.max() and Max.min() 获取一组数据中的最大值和最小值

console.log(Math.max(10,1,9,100,200,45,78));console.log(Math.min(10,1,9,100,200,45,78));

6,Math.PI 获取圆周率π 的值

console.log(Math.PI);

7,Math.pow() and Math.sqrt() 

Math.pow()获取一个值的多少次幂 
Math.sqrt()对数值开方

1.Math.pow(10,2) = 100;2.Math.sqrt(100) = 10;

JS中Math函数的常用方法

原文地址:https://www.cnblogs.com/wanguofeng/p/10504095.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved