JavaScript Math and Number objects, javascriptmath

Source: Internet
Author: User
Tags natural logarithm

JavaScript Math and Number objects, javascriptmath

1. Math object

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:

Copy codeThe 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:
 
Copy codeThe 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:

Copy codeThe 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:
 
Copy codeThe 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:
 
Copy codeThe 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:

Copy codeThe 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:

Copy codeThe 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:
 
Copy codeThe 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:
 
Copy codeThe 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
 
Copy codeThe 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:
 
Copy codeThe 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:
 
Copy codeThe 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:
 
Copy codeThe 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:
 
Copy codeThe 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:
 
Copy codeThe 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:
 
Copy codeThe 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:
 
Copy codeThe 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:
 
Copy codeThe 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:
 
Copy codeThe 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.

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.