1 Star 0 Fork 1

飘零雪/JavaScript基础学习笔记

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
87.math.html 2.48 KB
一键复制 编辑 原始数据 按行查看 历史
markrenChina 提交于 2021-05-29 23:11 +08:00 . first commit
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
/*
* Math
* - Math和其他的对象不同,它不是一个构造函数,
* 它属于一个工具类不用创建对象,它里边封装了数学运算相关的属性和方法
* - 比如
* Math.PI 表示的圆周率
*/
console.log(Math.PI); //3.141592653589793
/*
* abs()可以用来计算一个数的绝对值
*/
console.log(Math.abs(-100)); // 100
console.log(Math.abs(100)); // 100
/* * Math.ceil()
* - 可以对一个数进行向上取整,小数位只要有值就自动进1 */
console.log(Math.ceil(4.1)); // 5
/* * Math.floor()
* - 可以对一个数进行向下取整,小数部分会被舍掉 */
console.log(Math.floor(4.9)); // 4
/* * Math.round()
* - 可以对一个数进行四舍五入取整 */
console.log(Math.round(4.1)); // 4
console.log(Math.round(4.5)); // 5
/* * Math.random()
* - 可以用来生成一个0-1之间的随机数(不含0和1) */
for (var i = 0; i < 5; i++) {
console.log(Math.random());
}
/* * - 生成一个0-10的随机数 */
console.log(Math.random() * 10);
/* 生成0-10之间的整数,包含0和10 */
console.log(Math.round(Math.random() * 10));
/* * - 生成一个0-x之间的随机数 */
/* console.log(Math.round(Math.random()*x)); */
/* * - 生成一个1-10 之间的随机数*/
console.log(Math.round(Math.random() * 9) + 1);
/* * - 生成一个x-y之间的随机数 */
/* console.log(Math.round(Math.random()*(y-x))+x); */
/* 例子:生成5-10之间的随机数 */
for (var i = 0; i < 5; i++) {
console.log(Math.round(Math.random() * 5) + 5);
}
/*
* max() 可以获取多个数中的最大值
* min() 可以获取多个数中的最小值
*/
var max = Math.max(1, 5, 9, 15);
console.log(max); // 15
var min = Math.min(1, 5, 9, 15);
console.log(min); // 1
/*
* Math.pow(x,y)
* 返回x的y次幂
*/
console.log(Math.pow(2, 2)); // 4
console.log(Math.pow(2, 3)); // 8
/*
* Math.sqrt()
* 用于对一个数进行开方运算
*/
console.log(Math.sqrt(9)); // 3
console.log(Math.sqrt(7)); //2.6457513110645907
</script>
</head>
<body>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/zhenggb2010/java-script-notes.git
git@gitee.com:zhenggb2010/java-script-notes.git
zhenggb2010
java-script-notes
JavaScript基础学习笔记
master

搜索帮助