JavaScript Math and Number objects

Source: Internet
Author: User
Tags natural logarithm pow value of pi

1. Math Object

1.1 Introduction

The Math object is a mathematical object that provides mathematical calculations of the data, such as obtaining an absolute value, rounding up, and so on. No constructors, cannot be initialized, only static properties and methods are provided.

1.2 Constructors

None: The Math object has no constructors, cannot be initialized, and only provides 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 π. Returns the 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): Inverse cosine function

1.4.6 Math.atan (value): Inverse tangent function

1.4.7 Math.Abs (value): Returns the absolute value

Parameters:

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

return value:

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

Example:

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 pure numbers.

return value:

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

Example:

Math.ceil (2.7); = 3

Math.ceil (2.3); = = 3:2.3 rounding up returns 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 down a number is not rounded

Parameters:

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

return value:

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

Example:

Math.floor (2.7); = 2

Math.floor (2.3); = 2

Math.floor (-2.7); = 3:-2.7 Rounding down returns-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 pure numbers.

return value:

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

Example:

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 pure numbers.

return value:

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

Example:

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-order of X

Parameters:

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

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

return value:

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

Example:

Math.pow (2, 3); = 8:2 of the 3-time Square

Math.pow (3, 2); = 9:3 of the 2-time Square

Math.pow (' 4 ', 2); = 16:4 of the 2-time Square

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 greater than 0, less than 1.0

Example:

Math.random (); = 0.8982374747283757

Math.random (); = 0.39617531932890415

Math.random (); = 0.35413061641156673

Math.random (); = 0.054441051790490746

1.4.14 Math.Round (value): Rounding Back rounding

Parameters:

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

return value:

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

Example:

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 pure numbers

return value:

{Number} returns the square root of the parameter

Example:

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

The number object, which is a numeric object, contains integers, floating-point numbers, and so on in JS.

2.2 Definitions

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, approximately 5e-324

2.3.3 Number.NaN: Returns Nan, representing a non-numeric value, ranging from any other number, and also including the Nan itself. Number.isnan () should be used for judgment.

2.3.4 number.negative_infinity: Returns-infinity, which indicates negative infinity.

2.3.5 number.positive_infinity: Returns INFINITY, which represents positive infinity. The value that is calculated is greater than Number.MAX_VALUE and returns Infinity.

2.4 Static methods

2.4.1 Number.isinteger (value): Determines whether the parameter is an integer

Parameters:

①value {Number}: Numeric

return value:

{Boolean} returns whether the parameter is an integer. A string of pure integers also returns false.

Example:

Number.isinteger (1); = True

Number.isinteger (1.1); = False

Number.isinteger (' 1 '); = = False: A 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:

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): Converts a parameter to a floating-point number

Parameters:

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

return value:

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

Example:

Number.parsefloat (1); + = 1: integer or return integer

Number.parsefloat (1.1); = 1.1

Number.parsefloat (' 1aaa '); + 1: String preceded by a number, only returns a number

Number.parsefloat (' 1.1aaa '); = 1.1

Number.parsefloat (' A1 '); = = Nan: Non-numeric start, return 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 pure numbers

return value:

{integer} returns an integer value

Example:

Number.parseint (1); = 1

Number.parseint (1.1); + 1: Floating-point numbers return integers

Number.parseint (' 1aaa '); + 1: String preceded by a number, only returns a number

Number.parseint (' 1.1aaa '); = 1

Number.parseint (' A1 '); = = Nan: Non-numeric start, return Nan

Number.parseint (' a '); = NaN

2.5 Example methods

2.5.1 toexponential (value): Converts a number to an exponential type, and the argument represents the number of digits after the decimal point

Parameters:

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

return value:

{string} returns the converted exponential type string

Example:

(123456789). toexponential (2); + 1.23e+8: decimal 2 digits

(123456789). toexponential (5); + 1.23457e+8: decimal 5 digits

(123456789). toexponential (10); + 1.2345678900e+8: decimal 10 digits, less digits with 0 complement

2.5.2 toFixed (value): Converts a number to a string that specifies the number of decimal digits. Does not pass in the parameter, is not the decimal place. The return value is rounded

Parameters:

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

return value:

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

Example:

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: Rounding

2.5.3 toString (): Converts a number to a string using the specified binary. The parameter is not passed in, and the default is decimal.

Parameters:

①value {Number}: Represents a binary Value range: 2 to 36

return value:

{string} Converts a backward-based string

Example:

(Ten). ToString (); = 10: Default to Decimal

(Ten). toString (2); + = 1010: Binary

(Ten). toString (10); = 10: Decimal

(Ten). toString (16); = A: Hex

2.6 Application Scenarios

Subtraction exception of 2.6.1 floating-point number

Description: JS 2 Floating-point number for the subtraction operation, will return the value of the exception, such as: 0.2+0.7, return 0.899999999999. You can use the tofixed () method to specify decimal digits.

Example:

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, the value of the front and back will be converted to a value before the operation. If the conversion fails, a Nan is returned.

Example:

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: Call instance method after fast conversion to Nubmer object

Console.log (' 1 '-' a '); = = NaN: One party cannot convert to a Nubmer object

JavaScript Math and Number objects

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.