JavaScript Object notes --Math

A, Math Basic concepts

  • Math and various other objects, it is not a constructor
  • It belongs to the tools necessary to create an object,
  • Math package inside the properties and methods
  • Similar to C # static class

for example

<script type="text/javascript">
	//abs();绝对值
	console.log(Math.abs(-1));//输出1
</script>

Use Math object
Math. Method name

Two, Math objects common method

2.1 rounding

  • Math.ceil (); rounded up into decimal bit value is 1
  • Math.floor (); rounding down, give it decimal places
  • Math.round (); rounded to whole

operating

<script type="text/javascript">
	  console.log(Math.ceil(0.12));//输出1
	  console.log(Math.floor(0.12));//输出0
	  console.log(Math.round(0.12));//输出0
</script>

2.2 Random Number

  • Math.random (); generating a random number (excluding 01) between 0-1

For example

console.log(Math.random());//输出0.3419745734009261

Generating a random number of xy

  • Math.round(Math.Random()*(y-x)+x);

Examples 10 to generate random numbers 1-10

<script type="text/javascipt">
	for(var i=0;i<10;i++)
	{
		console.log(Math.round(Math.Random()*9+1));
	}
</script>
He published 198 original articles · won praise 94 · views 90000 +

Guess you like

Origin blog.csdn.net/shang_0122/article/details/104668173
Recommended