Common methods of JS Math object

Source: Internet
Author: User

Common methods of JS Math object
A set of numbers to find the maximum minimum value
var Max=math.max (1,2,3,4,8,-9);//ask for maximum value
var min=math.min (1,2,3,4,8,-9);//Minimum value
Console.log (max,min);//return 8-9 Note: Nan is returned if a string appears in the argument

Take the whole
var num=18.1;
var num2=18.9;
Rounding up Math.ceil ()
Console.log (Math.ceil (num), Math.ceil (num2));//return 19 19
Take only integer Math.floor ()
Console.log (Math.floor (num), Math.floor (num2));//return 18 18
Rounding the rounding Math.Round
Console.log (Math.Round (num), Math.Round (num2));//return 18 19

Absolute Math.Abs ()
var num =-9;
var num1 = 9;
Console.log (Math.Abs (num), Math.Abs (NUM1));//both return 9

Generate random number Math.random ()
var random=math.random ();
Console.log (random);//Returns a floating-point number greater than 0 less than 1 by default;
A random number method that generates an integer between A and B
function Getrandom (A, b) {
var num = b-a+1;//number of random numbers
Return Math.floor (Math.random () *num+a);
}

Common methods of JS Math object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.