# Introduction to JavaScriptMath and Number object_javascript skills

Source: Internet
Author: User

1.1 Introduction

A Math object is a mathematical object that provides mathematical calculations for data, such as getting absolute values and getting rounded up. No constructor and cannot be initialized. only static attributes and methods are provided.

1.2 constructor

None: The Math object has no constructor and cannot be initialized. only static attributes and methods are provided.

1.3 static attributes

1.3.1 Math. E: constant e. Returns the base number of the natural logarithm: 2.718281828459045

1.3.2 Math. PI: constant π. Returns the value of the circumference rate: 3.141592653589793

1.4 static method

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): arcsin function
1.4.5 Math. acos (value): arccosine function
1.4.6 Math. atan (value): arctangent 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 a parameter. If the parameter is not a number, NaN is returned.

Example:

The code is as follows:

H. abs ('20140901'); // => 123: a pure numeric string
Math. abs ('-123'); // => 123
Math. abs (123); // = & gt; 123
Math. abs (-123); // => 123
Math. abs ('123a '); // => NaN: a non-pure numeric string

1.4.8 Math. ceil (value): The number is rounded up, not rounded up.
Parameters:

① Value {Number | NumberStr}: a string of numbers or pure numbers.

Return value:

{Number} returns the rounded value. If the parameter is not a number, NaN is returned.

Example:

The code is as follows:

Math. ceil (2.7); // => 3
Math. ceil (2.3); // => 3: 2.3 rounded up return 3
Math. ceil (-2.7); // =>-2
Math. ceil (-2.3); // =>-2
Math. ceil ('2. 7'); // => 3: a string of only numbers.
Math. ceil ('2. 7a '); // => NaN: a non-pure numeric string

1.4.9 Math. floor (value): The number is rounded down, not rounded down.
Parameters:

① Value {Number | NumberStr}: a string of numbers or pure numbers.

Return value:

{Number} returns the rounded value. If the parameter is not a number, NaN is returned.

Example:

The code is as follows:

Math. floor (2.7); // => 2
Math. floor (2.3); // => 2
Math. floor (-2.7); // =>-3:-2.7 rounded down to return-3
Math. floor (-2.3); // =>-3
Math. floor ('2. 7'); // => 2: a string of only numbers.
Math. floor ('2. 7a '); // => NaN: a non-pure numeric string

1.4.10 Math. max (value1, value2... valueN): returns the maximum value of the 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, NaN is returned.

Example:

The code is 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 of the 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, NaN is returned.

Example:

The code is 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 Power y of x.
Parameters:

① X {Number | NumberStr}: string of numbers or pure numbers.

② Y {Number | NumberStr}: string of numbers or pure numbers.

Return value:

{Number} returns x to the power of y. If a parameter is not a number, NaN is returned.

Example:

The code is as follows:

Math. pow (2, 3); // => 8: Power 3 of 2
Math. pow (3, 2); // => 9: 2nd power of 3
Math. pow ('4', 2); // => 16: 2nd power of 4
Math. pow ('2a ', 2); // => NaN

1.4.13 Math. random (): returns a pseudo-random number greater than 0 and less than 1.0.
Parameter: None

Return value:

{Number} returns a pseudo-random Number greater than 0 and less than 1.0.

Example:

The code is 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 to an integer
Parameters:

① Value {Number | NumberStr}: a string of numbers or pure numbers.

Return value:

{Integer} returns the Integer after rounding the parameter. If the parameter is not a number, NaN is returned.

Example:

The code is 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: a string of only numbers.
Math. round ('2. 7a '); // => NaN: a non-pure numeric string

1.4.15 Math. sqrt (value): returns the square root of a parameter.
Parameters:

① Value {Number | NumberStr}: string of numbers or pure numbers

Return value:

{Number} returns the square root of the parameter.

Example:

The code is 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, which is a numeric object, including integers and floating-point numbers in js.

2.2 Definition

The code is as follows:

Var a = 1;
Var B = 1.1;

2.3 static attributes

2.3.1 Number. MAX_VALUE: indicates the largest Number in JS, which is about 1.79e + 308.

2.3.2 Number. MIN_VALUE: the smallest Number in JS, which is about 5e-324.

2.3.3 Number. NaN: return NaN, indicating a non-numeric value, which is different from any other Number, including NaN itself. Number. isNaN () should be used for determination.

2.3.4 Number. NEGATIVE_INFINITY:-Infinity is returned, indicating negative Infinity.

2.3.5 Number. POSITIVE_INFINITY: return Infinity, indicating positive Infinity. If the calculated value is greater than Number. MAX_VALUE, Infinity is returned.

2.4 static method

2.4.1 Number. isInteger (value): determines whether the parameter is an integer.
Parameters:

① Value {Number}: Number

Return value:

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

Example:

The code is as follows:

Number. isInteger (1); // => true
Number. isInteger (1.1); // => false
Number. isInteger ('1'); // => false: returns false for a string of pure integers.
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} indicates whether the returned parameter is NaN.

Example:

The code is 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): converts a parameter to a floating point Number.
Parameters:

① Value {Number | NumberStr}: string of numbers or pure numbers

Return value:

{Integer | Float} returns an Integer or floating-point number.

Example:

The code is as follows:

Number. parseFloat (1); // => 1: integer or return integer
Number. parseFloat (1.1); // = & gt; 1.1
Number. parseFloat ('1aaa'); // => 1: The string is preceded by a Number and only a Number is returned.
Number. parseFloat ('1. 1aaa'); // => 1.1
Number. parseFloat ('A1'); // => NaN: starts with a non-Number and returns NaN
Number. parseFloat ('A'); // => NaN

2.4.4 Number. parseInt (value): converts a parameter to an integer.
Parameters:

① Value {Number | NumberStr}: string of numbers or pure numbers

Return value:

{Integer} returns an Integer.

Example:

The code is as follows:

Number. parseInt (1); // => 1
Number. parseInt (1.1); // => 1: The floating point returns an integer.
Number. parseInt ('1aaa'); // => 1: the Number before the string. only the Number is returned.
Number. parseInt ('1. 1aaa'); // => 1
Number. parseInt ('A1'); // => NaN: starts with a Number and returns NaN
Number. parseInt ('A'); // => NaN

2.5 instance method

2.5.1 toExponential (value): convert a number to an exponential type. the parameter indicates the number of digits after the decimal point.
Parameters:

① Value {Number}: Number of digits after the decimal point

Return value:

{String} returns the converted exponential String.

Example:

The code is 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

2.5.2 toFixed (value): converts a number to a string with a specified decimal place. If no parameter is input, it means no decimal place. Returns a rounding value.
Parameters:

① Value {Number}: Number of digits after the decimal point

Return value:

{String} returns the converted String. if not enough decimal places are filled with 0, the returned value is the rounded value.

Example:

The code is as follows:

Console. log (1). toFixed (2); // = & gt; 1.00
Console. log (1.2). toFixed (2); // => 1.20: the number of digits that are insufficient. The value is 0.
Console. log (1.277). toFixed (2); // => 1.28: Rounding

2.5.3 toString (): convert a number to a string using the specified hexadecimal notation. If no parameter is input, the default value is decimal.
Parameters:

① Value {Number}: indicates the Number of hexadecimal values. value range: 2 to 36.

Return value:

{String} converted binary String

Example:

The code is as follows:

(10). toString (); // => 10: The default value is decimal.
(10). toString (2); // => 1010: Binary
(10). toString (10); // => 10: Decimal
(10). toString (16); // => a: Hexadecimal

2.6 application scenarios

2.6.1 The Addition, subtraction, multiplication, and division of floating point numbers are incorrect.
Note: If two floating point numbers in Js are used for addition, subtraction, multiplication, division, an exception value is returned, for example, 0.2 + 0.7, and 0.899999999999 is returned. You can use the toFixed () method to specify the decimal places.

Example:

The code is 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
// Use 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*10). toFixed (2); // => 30.30

2.6.2 subtraction
Note: When performing a subtraction operation in Js, the preceding and following values are converted to numerical values before the operation. If the conversion fails, NaN is returned.

Example:

The code is as follows:

Console. log ('1'-0); // => 1: a pure numeric string minus 0, which can be quickly converted to a Nubmer object.
Console. log ('1'-0). toFixed (2); // => 1.00: Call the instance method after the Nubmer object is quickly converted
Console. log ('1'-'A'); // => NaN: one party cannot be converted to an Nubmer object.

Related Keywords:

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.