数学相关方法
所有和操作数字相关的方法
通用语法: Math.xxx( )
1. random()
语法: Math.random()
返回值: 0 ~ 1 的随机小数, 可能包含 0, 但是绝对不可能包含 1
<!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>
</head>
<body>
<script> var res = Math.random();console.log(res) </script>
</body>
</html>
2. round()
+ 语法: Math.round(数字)
+ 返回值: 对该数字进行四舍五入的取整
+ 注意: 取整只看小数点后面一位
<!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>
</head>
<body>
<script> var res1 = Math.round(1.23223);console.log(res1) </script>
<script> var res2 = Math.round(1.663223);console.log(res2) </script>
<script> var res3 = Math.round(2.23223);console.log(res3) </script>
<script> var res = Math.round(0.23223);console.log(res) </script>
</body>
</html>
3. ceil()
+ 语法: Math.ceil(数字)
+ 返回值: 对该数字进行向上取整
<!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>
</head>
<body>
<script> var res1 = Math.ceil(1.23223);console.log('取值为',res1) </script>
<script> var res2 = Math.ceil(-1.663223);console.log('取值为',res2) </script>
<script> var res3 = Math.ceil(2.23223);console.log('取值为',res3) </script>
<script> var res = Math.ceil(-0.23223);console.log('取值为',res) </script>
</body>
</html>
4. floor()
+ 语法: Math.floor(数字)
+ 返回值: 对该数字进行向下取整
<!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>
</head>
<body>
<script> var res1 = Math.floor(1.23223);console.log('取值为',res1) </script>
<script> var res2 = Math.floor(-1.663223);console.log('取值为',res2) </script>
<script> var res3 = Math.floor(2.23223);console.log('取值为',res3) </script>
<script> var res = Math.floor(-0.23223);console.log('取值为',res) </script>
</body>
</html>
5. pow()
+ 语法: Math.pow(底数, 指数)
+ 语法: Math.pow(谁, 的多少次方)
+ 返回值: 取幂结果
<!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>
</head>
<body>
<script> var res1 = Math.pow(2,3);console.log('取值为',res1) </script>
<script> var res2 = Math.pow(-2,3);console.log('取值为',res2) </script>
</body>
</html>
6. sqrt()
+ 语法: Math.sqrt(数字)
+ 返回值: 该数字的算术平方根
<!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>
</head>
<body>
<script> var res1 = Math.sqrt(8);console.log('取值为',res1) </script>
<script> var res2 = Math.sqrt(122);console.log('取值为',res2) </script>
<script> var res3 = Math.sqrt(2);console.log('取值为',res3) </script>
<script> var res4 = Math.sqrt(5);console.log('取值为',res4) </script>
</body>
</html>
7. abs()
+ 语法: Math.abs(数字)
+ 返回值: 该数字的绝对值
<!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>
</head>
<body>
<script> var res1 = Math.abs(-8);console.log('取值为',res1) </script>
<script> var res2 = Math.abs(-12);console.log('取值为',res2) </script>
<script> var res3 = Math.abs(0.2);console.log('取值为',res3) </script>
<script> var res4 = Math.abs(-0.5);console.log('取值为',res4) </script>
</body>
</html>
8. max()
+ 语法: Math.max(数字1, 数字2, 数字3, ...)
+ 返回值: 若干个数字中的最大值
<!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>
</head>
<body>
<script> var res1 = Math.max(-8,0,1,2,5,4,16);console.log('取值为',res1) </script>
</body>
</html>
9. min()
+ 语法: Math.min(数字1, 数字2, 数字3, ...)
+ 返回值: 若干个数字中的最小值
<!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>
</head>
<body>
<script> var res1 = Math.min(-8,0,1,2,5,4,16);console.log('取值为',res1) </script>
</body>
</html>
10. PI
+ 是一个属性, 不是方法, 使用不需要加 ()
+ 语法: Math.PI
+ 得到: 一个近似 π 的值
<!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>
</head>
<body>
<script> var res1 = Math.PI ;console.log('取值为',res1) </script>
</body>
</html>
11. 保留小数位
+ 保留多少位小数
+ 语法: 数字.toFixed(你要保留多少位小数)
+ 返回值: 保留好小数位的 字符串, 如果小数位不够, 使用 0 补齐
+ 注意: 返回值是一个字符串类型
+ 保留 x 位小数, 一定是看 x+1 位小数是多少, 进行四舍五入
<!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>
</head>
<body>
<script>
var res1 = Math.PI ;
console.log('取值为',res1)
// 取小数位
var res2= res1.toFixed(2) ; console.log(res2)
var res3= res1.toFixed(3) ; console.log(res3)
var res4= res1.toFixed(4) ; console.log(res4)
var res5= res1.toFixed(5) ; console.log(res5)
</script>
</body>
</html>