数学对象

JavaScript数学方法详解

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<script>
//取绝对值
var re = Math.abs(-2.4);
console.log(re);
//取近似整数(如果是整数,向上向下取舍还是它本身)
//四舍五入(四舍五入的结果一定是整数)
var re1 = Math.round(2.4);
console.log(re1);
//对整数进行向下取舍
var re2 = Math.floor(2.4);
console.log(re2);
//对整数进行向上取舍
var re3 = Math.ceil(2.4);
console.log(re3);
//取随机数
//Math.random() (取0-1之间的随机数,取值范围无限接近0) 取x到y之间的随机数公式:Math.random()*((y-x)+x); 取随机整数的公式:Math.floor(Math.random()*((y+1-x)+x));
//例子:我们通常抽奖的概率问题,就拿中与不中两个来比例
var num = Math.random() * (10 - 1 + 1);
if (num < 8) {
console.log("不中");
} else {
console.log("中");
}
//同类例子 以数组形式表现
var arr = ["中", "不中", "不中", "不中", "不中", "不中", "不中", "不中", "不中", "不中", "中", "不中", "中", "不中"];
var result = Math.floor(Math.random() * (arr.length - 1 + 1));
console.log(arr[result]);
</script>
</body>
</html>

转载于:https://www.cnblogs.com/qwfy929295660/p/7123955.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值