• 【JavaScript】Math对象知识全解


    前言

    除了简单的加减乘除,在某些长和开发者需要进行更为复杂的数学运算。JavaScript的Math对象提供了一系列属性和方法,能够满足大多数场合的需求。
    Math对象是JavaScript的全局对象,不需要由函数进行创建。有且只有一个Math对象。

    常用属性

    属性说明
    Math.E返回值e(自然对数的底数)
    Math.LN10返回10的自然对数
    Math.LN2返回2的自然对数
    Math.LOG2E返回以2为底的e的对数
    Math.LOG10E返回以10为底的e的对数
    Math.PI返回圆周率π
    Math.SQRT1_2返回1/2的平方根
    Math.SQRT2返回2的平方根

    常用方法

    方法说明
    Math.abs(x)返回x的绝对值
    Math.acos(x)返回x的反余弦值,其中x的范围为[-1,1],返回值的范围为[0,π]
    Math.asin(x)返回x的反正弦值,其中x的范围为[-1,1],返回值的范围为[-π/2,π/2]
    Math.atan(x)返回x的反正切值,返回值的范围为[-π/2,π/2]
    Math.atan2(y,x)返回原点和点(x,y)的连线与x正半轴的夹角,夹角范围为[-π,π]
    Math.cos(x)返回x的余弦值
    Math.exp(x)返回e的x次方
    Math.log(x)返回x的自然对数
    Math.pow(x)返回x的y次方
    Math.sin(x)返回x的正弦值
    Math.sqrt(x)返回x的平方根,x必须大于或等于0
    Math.tan(x)返回x的正切值

    实例参考

    静态常量值PI(圆周率

    DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>Math对象title>
    	head>
    	<body>
    		<script>
    			//静态常量值PI(圆周率)
    			var result1 = Math.PI;
    			console.log(result1);
    		script>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    abs(x) 获取绝对值

    DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>Math对象title>
    	head>
    	<body>
    		<script>
    			console.log(Math.abs(12));
    			console.log(Math.abs(-12));
    		script>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    ceil(x)上舍入,返回大于x的值中的最小整数值

    DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>Math对象title>
    	head>
    	<body>
    		<script>
    			console.log(Math.ceil(5.1));//6
    			console.log(Math.ceil(5.4));//6
    			console.log(Math.ceil(5.5));//6
    			console.log(Math.ceil(5.9));//6
    		script>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    floor(x)下舍入,返回小于x的值中的最大整数值

    DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>Math对象title>
    	head>
    	<body>
    		<script>
    			console.log(Math.floor(5.9));//5
    			console.log(Math.floor(5.5));//5
    			console.log(Math.floor(5.4));//5
    			console.log(Math.floor(5.1));//5
    		script>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    round()四舍五入,小于0.5的舍去,大于等于0.5,进一

    DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>Math对象title>
    	head>
    	<body>
    		<script>
    			console.log(Math.round(5.1))//5
    			console.log(Math.round(5.4))//5
    			console.log(Math.round(5.5))//6
    			console.log(Math.round(5.9))//6
    		script>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    Math.random():返回一个[0.0,1.0)之间的浮点数

    DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>Math对象title>
    	head>
    	<body>
    		<script>
    		    //Math.random():返回一个[0.0,1.0)之间的浮点数
    			console.log(Math.random());
    			//Math.random()*10:返回一个[0.0,10.0)之间的浮点数
    			console.log(Math.random()*10);
    			//parseInt(Math.random()*10):返回一个[0,10)之间的整数
    			console.log(parseInt(Math.random()*10));
    		script>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    parseInt(Math.random()*(num2-num1)+num1):返回一个[num1,num2)之间的整数

    DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>Math对象title>
    	head>
    	<body>
    		<script>
    			console.log(parseInt(Math.random()*22+39));
    		script>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    max(数据1,数据2,…,数据n):获取多个数据中的最大值

    DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>Math对象title>
    	head>
    	<body>
    		<script>
    			console.log(Math.max(10,23,52));
    		script>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    min(数据1,数据2,…,数据n):获取多个数据中的最小值

    DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>Math对象title>
    	head>
    	<body>
    		<script>
    			console.log(Math.min(10,23,65,3,89));//3
    		script>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    pow(x,y):获取x的y次方结果

    DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>Math对象title>
    	head>
    	<body>
    		<script>
    			console.log(Math.pow(9,3));
    		script>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    sqrt(x):返回x的平方根

    DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>Math对象title>
    	head>
    	<body>
    		<script>
    			console.log(Math.sqrt(25));//5
    		script>
    	body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    .NET分布式Orleans - 9 - 贪吃蛇项目演示
    存储优化知识复习二详细版解析
    springboot使用@Validated校验不生效
    【Android进阶】14、顶部应用栏
    ES7 集群模式新增账号密码认证
    JS中面向对象的程序设计
    到什么程度才叫精通 Linux?
    SQL Server进阶知识
    Jmeter二次开发实现rsa加密
    Linux高性能服务器编程——ch6笔记
  • 原文地址:https://blog.csdn.net/hh867308122/article/details/127769263