JavaScript rounding, taking remainder math method

Source: Internet
Author: User
Tags natural logarithm square root

1. Discard the fractional part, preserving the whole number of parts

The parseint () function parses a string and returns an integer.

parseint (string, radix)

parameters
string
radix

optional. Represents the cardinality of the number to parse. The value is between 2 and 36.

parseint ("ten");    // return ten // returned (10+9)parseint ("One", 2);  // returns 3 (2+1)parseint ("n", 8);  // back to (8+7) // return to (16+15)parseint ("010");   // undecided: Returns 10 or 8

2. Rounding up, with decimals, the integer part plus 1

The Ceil () method performs an up-rounding calculation, which returns an integer that is greater than or equal to the function parameter and closest to it.

Math.ceil (0.60)   //  1math.ceil (0.40)   //  1math.ceil (5)      // 5 Math.ceil (5.1)    //  6math.ceil ( -5.1)   //  -5Math.ceil (-5.9)   //  -5

3. Rounding

The round () method rounds a number to the nearest integer.

Math.Round (0.60)  //  1math.round (0.40)  //  0math.round (5)     //  5math.round (5.1)   //  5math.round ( -5.1)  //  -5 Math.Round ( -5.9)  //  -6

4. Rounding Down

The floor () method performs a down-rounding calculation, which returns an integer that is less than or equal to the function parameter and closest to it.

Math.floor (0.60)  //  0Math.floor (0.40)  //  0Math.floor (5)     //  5Math.floor (5.1)   //  5math.floor ( -5.1)  //  -6 Math.floor ( -5.9)  //  -6

5. Take the remainder

0%4  //  01%4  //  12%4  //  23%4  // 34%4  //  05%4  //  1
6.Math Method ExtensionThe 6.1 max () method, which compares the maximum value in a set of values, returns the maximum value.
var maxnum = Math.max (12,6,43,58,70//"

The 6.2 min () method, which compares the minimum value in a set of values, returns the minimum value.

var minnum = math.min (12,6,43,58,70//"6"

7. Generating a random number method
The random () method, which returns a number greater than or equal to 0 less than 1, is actually a decimal value.
The random number range is expressed in intervals of [0,1], which contains 0 without 1.

Here's a formula:
The value returned = Math.floor (math.random () * Total number of possible values + first possible value),
Apply this formula to return a random number within an integer range.
We noticed that the floor () method was called in the formula above, because random () always returns a decimal, and we want to return an integer, so we have to round up the return value.


For example, we want to randomly produce integers between 1 and 10, [1,10], which contain 1 and 10, and you can write the following code in a formula:

var num = Math.floor (math.random () * 10 + 1);

[1,10] contains 10 numbers, so the total number of possible values is 10, then the random () *10, that is, the generation [0,10] between the number of randomly;
The first possible value is 1, then random () *10+1, that is, the generation [1,11] between the stochastic number;
At this point it produces the smallest integer is 1, but the largest integer is not 10, it will produce a 10.xxxxxxxxxx such a decimal, and then use floor () to round it down, so that it produces the largest integer is 10.

If you want to randomly generate integers between [5,10], you can write code like this by applying a formula:

var num = Math.floor (Math.random () * 6 + 5);

[5,10] contains 6 numbers, so the total number of possible values is 6, the random () *6, the first possible value is 5, so the random () *6+5, and finally the floor () to round it down.

8. Other methods of calculation

The following methods are used for a variety of simple or complex calculations.

Math.Abs (num) returns the absolute value of num

MATH.EXP (NUM) returns the NUM power of the MATH.E

Math.log (num) returns the natural logarithm of num

Math.pow (Num,power) returns the power sub power of num

MATH.SQRT (num) returns the square root of num

Math.acos (x) returns the inverse cosine value of x

Math.asin (x) returns the inverse sine value of x

Math.atan (x) returns the inverse tangent value of x

Math.atan2 (y,x) returns the inverse tangent value of y/x

Math.Cos (x) returns the cosine of X

Math.sin (x) returns the sinusoidal value of x

Math.tan (x) returns the tangent of X

JavaScript rounding, taking remainder math method

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.