JavaScript Math and Number objects

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

Math object: A Mathematical object that provides a mathematical calculation of the data. such as: Get absolute value, rounding up and so on. No constructors, cannot be initialized, only static properties and methods are provided.

Number object: The object that provides numbers in JS. contains integers, floating-point numbers, and so on. and provide a number of type conversion, decimal point interception and other methods.

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.7Math.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:

Math.Abs (' 123 '); = 123: Pure numeric string Math.Abs ('-123 '); = 123math.abs (123); = 123 Math.Abs (-123); = = 123math.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); = 3math.ceil (2.3); = 3:2.3 rounding Up returns 3math.ceil (-2.7); = -2math.ceil (-2.3); = = -2math.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); = 2math.floor (2.3); = 2math.floor (-2.7); = 3:-2.7 Rounding down returns -3math.floor (-2.3); = = -3math.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:

  

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:

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 3 square Math.pow (3, 2); = 9:3 of 2 square math.pow (' 4 ', 2); = 16:4 of 2 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:

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); = 3math.round (2.4); = 2math.round (-2.6); =-3 Math.Round (-2.5); =-2:-2.5 rounded to -2math.round (-2.4); = = -2math.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)); = 3console.log (Math.sqrt (16)); = = 4console.log (math.sqrt (' 25 ')); = = 5console.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 method 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 Falsenumber.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:

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:

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:

2.5 instance Method 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 bits (123456789). toexponential (5); = 1.23457e+8: decimal 5 bits (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: The default is decimal (Ten). toString (2); = 1010: Binary (Ten). toString (10); + 10: Decimal (Ten). toString (16); = A: Hex

  

2.6 Application Scenario 2.6.1 floating-point subtraction exception

Description: JS 2 Floating-point number for 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.8999999999999999console.log (0.7-0.5); = 0.19999999999999996 Console.log (3.03 * 10); = = 30.299999999999997//Use the toFixed () method Console.log ((0.2 + 0.7). ToFixed (2)); = 0.90 Console.log ((0.7-0.5) toFixed (2)); = 0.20  

  

2.6.2 Subtraction Operation

Description: 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 Console.log (' 1 '-' a ') after fast conversion to Nubmer object; = = NaN: One party cannot convert to a Nubmer object

================================== Series article ==========================================

This article: 3.5 JavaScript Math and Number Object

Web Development Road Series articles

JavaScript Math and Number objects

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.