JavaScript Math Object (Math)

1、Math

  • Math is a built-in object, it has some mathematical constant properties and mathematical function methods, Math is not a function object
    ————— quoted from the official website

2. Description

  • Unlike other global objects, Math is not a constructor. All properties and methods of Math are static. The way to quote pi is Math.PI, and the way to call the sine and cosine function is Math.sin(x), where x is the parameter to be passed in. Math constants are defined using full-precision floating-point numbers in JavaScript.
    ————— Quoted from the official website

3. Method

(1) Obtain random numbers

  • The random number returns a number between 0 and 1 by default

① Syntax format:

	Math.random();

② Example:

	//这里为了方便展示效果,用for循环输出10次随机数
	<script>
		let Num;
		for (i = 1;i <= 10;i++){
    
    
			Num = Math.random();
			console.log(Num);
		}
	</script>

③ Operation effect

Picture

(2) Obtain an integer random number

Method 1 (rounding)
  • parseInt(Math.random()*(maximum-minimum+1))+minimum
Method 2 (round down)
  • Math.floor(Math.random()*(maximum-minimum+1))+minimum

① Syntax format:

  • Get a random number from 1 to 10
	//取整
	parseInt(Math.random() * (最大值 - 最小值 + 1 )) + 最小值;

	//向下取整
	Math.floor(Math.random() * (最大值 - 最小值 + 1)) + 最小值;

② Example:

	//取整,用for循环取10次随机数
	<script>
		let Num;
		for (i = 1;i <= 10;i++){
    
    
			//公式:parseInt(Math.random() * (最大值 - 最小值 + 1)) + 最小值
			Num = parseInt(Math.random() * (10 - 1 + 1)) + 1;
			//最小值为1的时候可以简写:Num = parseInt(Math.random() * 10) + 1
			document.write(Num + ' ');
		}
	</script>

running result

Picture

	//向下取整,用for循环取10次随机数
	<script>
		let Num;
		for (i = 1;i <= 10;i++){
    
    
			//公式:Math.floor(Math.random() * (最大值 - 最小值 + 1)) + 最小值;
			Num = Math.floor(Math.random() * (10 - 1 + 1)) + 1;
			//最小值为1的时候可以简写:Num = parseInt(Math.random() * 10) + 1
			document.write(Num + ' ');
		}
	</script>

running result

Picture

(3) Rounding

① Syntax format:

	Math.round();

② Example:

	<script>
		let NumOne = Math.round(7.56);
		let NumTwo = Math.round(7.05);
		document.write(`
		7.56四舍五入等于:${
      
      NumOne} <br />
		7.05四舍五入等于:${
      
      NumTwo}
		`);
		// <br />:换行
	</script>

③ Operation effect

Picture

(4) Round down

① Syntax format:

	Math.floor();

② Example:

	<script>
		let NumOne = Math.floor(7.55);
		let NumTwo = Math.floor(7.05);
		document.write(`
		7.55向下取整等于:${
      
      NumOne} <br />
		7.05向下取整等于:${
      
      NumTwo}
		`);
		// <br />:换行
	</script>

③ Operation effect

Picture

(5) Round up

① Syntax format:

	Math.ceil();

② Example:

	<script>
		let NumOne = Math.ceil(7.55);
		let NumTwo = Math.ceil(7.05);
		document.write(`
		7.55向上取整等于:${
      
      NumOne} <br />
		7.05向上取整等于:${
      
      NumTwo}
		`);
		// <br />:换行
	</script>

③ Operation effect

Picture

(6) power

① Syntax format:

	Math.pow(底数,指数/);

② Example:

	<script>
		//计算2的三次方
		let NumOne = Math.pow(2,3);
		//计算3的三次方
		let NumTwo = Math.pow(3,3);
		document.write(`
		2的三次方等于:${
      
      NumOne} <br />
		3的三次方等于:${
      
      NumTwo}
		`)
	</script>

③ Operation effect

Picture

(7) Absolute value

① Syntax format:

	Math.abs();

② Example:

	<script>
		let NumOne = Math.abs(-100);
		let NumTwo = Math.abs(-200);
		document.write(`
		-100的绝对值等于:${
      
      NumOne} <br />
		-200的绝对值等于:${
      
      NumTwo}
		`)
	</script>

③ Operation effect

Picture

(8) Square root

① Syntax format:

	Math.sqrt();

② Example:

	<script>
		let NumOne = Math.sqrt(9);
		let NumTwo = Math.sqrt(25);
		let NumThr = Math.sqrt(81);
		document.write(`
		9的平方根等于:${
      
      NumOne} <br />
		25的平方根等于:${
      
      NumTwo} <br />
		81的平方根等于:${
      
      NumThr}
		`)
	</script>

③ Operation effect

Picture

(9) Maximum/minimum value

① Syntax format:

	//最大值
	Math.max();
	
	//最小值
	Math.min();

② Example:

	<script>
		let NumMax = Math.max(1,11,111,1111,11111);
		let NumMin = Math.min(1,11,111,1111,11111);
		document.write(`
		最大值为:${
      
      NumMax} <br />
		最小值为:${
      
      NumMin}
		`)
	</script>

③ Operation effect

Picture

For more mathematical object learning, please refer to the official document

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math

Guess you like

Origin blog.csdn.net/StupidlyGrass/article/details/128713433