On _javascript skills of JavaScript math and number objects

Source: Internet
Author: User
Tags abs constant instance method numeric natural logarithm pow square root value of pi

1. Math Object

1.1 Introduction

The Math object, which is a mathematical object, provides a mathematical calculation of the data, such as acquiring absolute value, rounding up, and so on. No constructors, cannot be initialized, provides only static properties and methods.

1.2 Constructors

None: The Math object has no constructors, cannot be initialized, and provides only static properties and methods.

1.3 Static properties

1.3.1 MATH.E: Constant E. Returns the base of the natural logarithm: 2.718281828459045

1.3.2 Math.PI: Constant π. return value of pi: 3.141592653589793

1.4 Static Methods

1.4.1 Math.sin (value): sine function
1.4.2 Math.Cos (value): cosine function
1.4.3 Math.tan (value): Tangent function
1.4.4 Math.asin (value): Inverse chord function
1.4.5 Math.acos (value): Anti-cosine function
1.4.6 Math.atan (value): Inverse tangent function
1.4.7 Math.Abs (value): Returning absolute value

Parameters:

①value {Number | NUMBERSTR}: A string of numbers or plain numbers.

return value:

{Number} returns the absolute value of the parameter. Returns Nan If the argument is not a number.

Example:

Copy Code code as follows:

H.abs (' 123 '); => 123: Pure numeric string
Math.Abs ('-123 '); => 123
Math.Abs (123); => 123
Math.Abs (-123); => 123
Math.Abs (' 123a '); => NaN: Non-pure numeric string

1.4.8 Math.ceil (value): Rounding a number up, not rounded
Parameters:

①value {Number | NUMBERSTR}: A string of numbers or plain numbers.

return value:

{Number} returns the value after rounding. Returns Nan If the argument is not a number.

Example:

Copy Code code as follows:

Math.ceil (2.7); => 3
Math.ceil (2.3); => 3:2.3 up, rounding back 3.
Math.ceil (-2.7); =>-2
Math.ceil (-2.3); =>-2
Math.ceil (' 2.7 '); => 3: Pure numeric string
Math.ceil (' 2.7a '); => NaN: Non-pure numeric string


1.4.9 Math.floor (value): Rounding a number down, not rounded
Parameters:

①value {Number | NUMBERSTR}: A string of numbers or plain numbers.

return value:

{Number} returns the value after rounding. Returns Nan If the argument is not a number.

Example:

Copy Code code as follows:

Math.floor (2.7); => 2
Math.floor (2.3); => 2
Math.floor (-2.7); =>-3:-2.7 down-rounding return-3
Math.floor (-2.3); =>-3
Math.floor (' 2.7 '); => 2: Pure numeric string
Math.floor (' 2.7a '); => NaN: Non-pure numeric string


1.4.10 Math.max (Value1,value2...valuen): Returns the largest value in a parameter
Parameters:

①value1,value2.....valuen {Number | NUMBERSTR}: A string of numbers or plain numbers.

return value:

{Number} returns the maximum value. If a parameter is not a number, return Nan.

Example:

Copy Code code as follows:

Math.max (1, 2, 3, 4, 5); => 5
Math.max (1, 2, 3, 4, ' 5 '); => 5
Math.max (1, 2, 3, 4, ' a '); => NaN
  

1.4.11 math.min (Value1,value2...valuen): Returns the smallest value in a parameter
Parameters:

①value1,value2.....valuen {Number | NUMBERSTR}: A string of numbers or plain numbers.

return value:

{Number} returns the maximum value. If a parameter is not a number, return Nan.

Example:

Copy Code code as follows:

Math.min (1, 2, 3, 4, 5); => 1
Math.min (' 1 ', 2, 3, 4, 5); => 1
Math.min (1, 2, 3, 4, ' a '); => NaN


1.4.12 Math.pow (X,y): Returns the Y-side of X
Parameters:

①x {Number | NUMBERSTR}: A string of numbers or plain numbers.

②y {Number | NUMBERSTR}: A string of numbers or plain numbers.

return value:

{Number} returns the Y-second side of X. If a parameter is not a number, return Nan.

Example:

Copy Code code as follows:

Math.pow (2, 3); => 8:2 of the 3-time party
Math.pow (3, 2); => 9:3 of the 2-time party
Math.pow (' 4 ', 2); => 16:4 of the 2-time party
Math.pow (' 2a ', 2); => NaN


1.4.13 math.random (): Returns a pseudo random number, greater than 0, less than 1.0
Parameters: None

return value:

{Number} returns a pseudo random count of more than 0, less than 1.0

Example:

Copy Code code as follows:

Math.random (); => 0.8982374747283757
Math.random (); => 0.39617531932890415
Math.random (); => 0.35413061641156673
Math.random (); => 0.054441051790490746


1.4.14 Math.Round (value): Rounded after rounding
Parameters:

①value {Number | NUMBERSTR}: A string of numbers or plain numbers.

return value:

{integer} returns an integer after the parameter is rounded. Returns Nan If the argument is not a number.

Example:

Copy Code code as follows:

Math.Round (2.5); => 3
Math.Round (2.4); => 2
Math.Round (-2.6); =>-3
Math.Round (-2.5); =>-2:-2.5 rounded to-2
Math.Round (-2.4); =>-2
Math.Round (' 2.7 '); => 3: Pure numeric string
Math.Round (' 2.7a '); => NaN: Non-pure numeric string


1.4.15 math.sqrt (value): Returns the square root of a parameter
Parameters:

①value {Number | NUMBERSTR}: A string of numbers or plain numbers

return value:

{Number} returns the square root of the parameter

Example:

Copy Code code as follows:

Console.log (MATH.SQRT (9)); => 3
Console.log (MATH.SQRT (16)); => 4
Console.log (math.sqrt (' 25 ')); => 5
Console.log (Math.sqrt (' a ')); => NaN

2. Number Object

2.1 Introduction

Number object is a numeric object that contains integers, floating-point numbers, and so on in JS.

2.2 Definition

Copy Code code as follows:

var a = 1;
var B = 1.1;

2.3 Static Properties

2.3.1 Number.MAX_VALUE: Represents the largest number in JS, about 1.79e+308

2.3.2 Number.min_value: Represents the smallest number in JS, about 5e-324

2.3.3 Number.NaN: Returns Nan, indicating non-numeric values, which range from any other number, including the Nan itself. The Number.isnan () should be used for judgment.

2.3.4 number.negative_infinity: Returns-infinity, representing negative infinity.

2.3.5 number.positive_infinity: Returns INFINITY, representing positive infinity. Returns Infinity if the value being calculated is greater than number.max_value.

2.4 Static methods

2.4.1 Number.isinteger (value): Determining whether an argument is an integer
Parameters:

①value {Number}: Digital

return value:

{Boolean} returns whether the argument is an integer. The string of a pure integer also returns false.

Example:

Copy Code code as follows:

Number.isinteger (1); => true
Number.isinteger (1.1); => false
Number.isinteger (' 1 '); => false: string of pure integers also returns false
Number.isinteger (' 1.1 '); => false
Number.isinteger (' a '); => false: Non-string return false


2.4.2 Number.isnan (value): Determines whether the parameter is Nan
Parameters:

①value {Object}: any type

return value:

{Boolean} returns whether the parameter is Nan.

Example:

Copy Code code as follows:

Number.isnan (NaN); => true
Number.isnan (' NaN '); => false: ' Nan ' string, not Nan
Number.isnan (1); => false
Number.isnan (' 1 '); => false


2.4.3 Number.parsefloat (value): Converting parameters to floating point numbers
Parameters:

①value {Number | NUMBERSTR}: A string of numbers or plain numbers

return value:

{Integer | FLOAT} returns an integer or floating-point number

Example:

Copy Code code as follows:

Number.parsefloat (1); => 1: integer or return integer
Number.parsefloat (1.1); => 1.1
Number.parsefloat (' 1aaa '); => 1: The string is preceded by a number, and only the number is returned
Number.parsefloat (' 1.1aaa '); => 1.1
Number.parsefloat (' A1 '); => nan: Not beginning with a number, returning Nan
Number.parsefloat (' a '); => NaN


2.4.4 Number.parseint (value): Converts a parameter to an integer
Parameters:

①value {Number | NUMBERSTR}: A string of numbers or plain numbers

return value:

{integer} returns an integer value

Example:

Copy Code code as follows:

Number.parseint (1); => 1
Number.parseint (1.1); => 1: Floating-point numbers return integers
Number.parseint (' 1aaa '); => 1: The string is preceded by a number, and only the number is returned
Number.parseint (' 1.1aaa '); => 1
Number.parseint (' A1 '); => nan: Not beginning with a number, returning Nan
Number.parseint (' a '); => NaN

2.5 Instance Methods

2.5.1 toexponential (value): Converts a number to an exponential type, with a parameter representing the number of digits after the decimal point
Parameters:

①value {Number}: Indicates digits after the decimal point

return value:

{string} returns the converted exponential type string

Example:

Copy Code code as follows:

(123456789). toexponential (2); => 1.23e+8:2 decimal places
(123456789). toexponential (5); => 1.23457e+8:5 decimal places
(123456789). toexponential (10); => 1.2345678900e+8: The decimal point 10 digits, the number of insufficient digits with 0 complement


2.5.2 toFixed (value): Converts a number to a string that specifies the number of decimal places. Without passing in the argument, there is no decimal digit. The return value is rounded
Parameters:

①value {Number}: Indicates digits after the decimal point

return value:

{string} returns the converted string; not enough decimal places to fill with 0; The return value is rounded

Example:

Copy Code code as follows:

Console.log ((1). toFixed (2)); => 1.00
Console.log ((1.2). ToFixed (2)); => 1.20: Insufficient digits, with 0 complement
Console.log ((1.277). toFixed (2)); => 1.28: Rounded up


2.5.3 toString (): Converts a number to a string using the specified system. No parameters are passed in, and the default is decimal.
Parameters:

①value {Number}: Represents the numbers, values range: 2 to 36

return value:

{string} converts a backward string

Example:

Copy Code code as follows:

(a). ToString (); => 10: The default is decimal
(a). ToString (2); => 1010: Binary
(a). ToString (10); => 10: Decimal
(a). ToString (16); => A: Hex

2.6 Application Scenarios

Subtraction anomaly of 2.6.1 floating-point numbers
Note: JS 2 floating-point numbers in the subtraction operation, will return the value of the exception, such as: 0.2+0.7, returned 0.899999999999. You can use the tofixed () method to specify a decimal place.

Example:

Copy Code code as follows:

Console.log (0.2 + 0.7); => 0.8999999999999999
Console.log (0.7-0.5); => 0.19999999999999996
Console.log (3.03 * 10); => 30.299999999999997
Using the Tofixed () method
Console.log ((0.2 + 0.7). ToFixed (2)); => 0.90
Console.log ((0.7-0.5). toFixed (2)); => 0.20
Console.log ((3.03 *). ToFixed (2)); => 30.30


2.6.2 Subtraction Operation
Note: JS in the subtraction operation, will first the value before and after conversion to the number and then operation. If the conversion fails, return Nan.

Example:

Copy Code code as follows:

Console.log (' 1 '-0); => 1: Pure numeric string minus 0, can be quickly converted to Nubmer object
Console.log (' 1 '-0). toFixed (2)); => 1.00: Invoke instance method after fast conversion to Nubmer object
Console.log (' 1 '-' a '); => NaN: One side cannot be converted to a Nubmer object

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.