How to use Math as a built-in javascript Object

Source: Internet
Author: User

1. Math. min () and Math. max () return the minimum and maximum values of the parameters respectively.
Example:
Alert (Math. min (1, 2, 3) // output "1"
Alert (Math. max (1, 2, 3) // output "3"

2. Math. abs () returns the absolute value of the parameter.
Example:
Alert (Math. abs (-1) // output "1"

3. Math. random () generates a random number ranging from 0 to 1.
Example:
Window. open ("http: // www. ***. com/index.shtml? T = "+ Math. random) // Add a parameter after the url address with the value of" Number "to ensure that the page is pulled from the server every time, instead of reading the cache.

4. Math. floor (), Math. round (), Math. ceil ()
Math. floor (): round down the decimal point to an integer. Example: alert (Math. floor (1.5) // output "1"
Math. round (): round the decimal number to an integer. Example: alert (Math. round (1.5) // output "2"
Math. ceil (): round up the decimal point to an integer. Example: alert (Math. round (1.5) // output "2"
Using these three functions is very convenient when it involves decimal calculation. For example, the following function is designed for decimal processing.
Copy codeThe Code is as follows:
Function test (num, flag, bit) // The number of decimals to be passed in (-1, down; 0, standard; 1) "flag" Reserved decimal digits "bit"
{
Var n = Math. pow (10, bit );
Switch (flag)
{
Case-1: return Math. floor (num * n)/n; break;
Case 0: return Math. round (num * n)/n; break;
Case 1: return Math. ceil (num * n)/n;
}
}

Related Article

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.