Details about the JS value Number type, and details about the jsnumber

Source: Internet
Author: User
Tags mathematical constants natural logarithm string to number

Details about the JS value Number type, and details about the jsnumber
Number Problems

Are you able to answer the following questions correctly?

  • 0.1 + 0.2 = 0.3?
  • How much does the. E-5 represent?
  • How does one represent an octal format?
  • How to convert the hexadecimal format?
  • How do I convert a string to a value or integer? In turn? What should I do with the hexadecimal system?
  • What is the return value of parseInt (0x12, 16? Is it 0x12?
  • Number. MAX_VALUE is the maximum value (new Number (12). What is MAX_VALUE?
  • In JavaScript, how does one perform rounding? What if I keep the precision of 3 decimal places?
  • How to obtain a random number? How to get an integer? How can I rounded up?
Number representation

Number indicates a Number. JavaScript uses the double-precision 64-bit format defined in IEEE 754 ("double-precision 64-bit format IEEE 754 values") to indicate a Number.

Unlike other programming languages (such as C and Java), JavaScript does not distinguish between integer and floating point values. All numbers are represented by floating point values in JavaScript, therefore, when performing numeric operations, pay special attention to the lack of progress.

0.1 + 0.2 = 0.30000000000000004; 0.1 + 0.2 = 0.3; // false // determine equal var ACCURACY = 1e-5 for floating point operations; // The definition ACCURACY is accurate to 0.20.1var a = 0.1; var B = 0.2; var sum = 0.3; // if (a + B-sum <ACCURACY) {console. log ('a + B = Sum ');}

In specific implementation, the integer value is usually considered as a 32-bit integer variable, and is also stored in the form of 32-bit integer variables in some implementations (such as some browsers, it is used to perform operations that are not supported by 32-bit Integers to facilitate bitwise operations.

You can omit 0 to indicate decimal places or numbers in exponential form.

.9; // 0.91E3 // 10002e6 // 20000000.1e2 // 101e-5 // 0.00001
Number numbers in different hexadecimal formats

Representation of different hexadecimal formats

Number can be in decimal, binary, octal, or hexadecimal notation. Only integers are used in non-decimal format.

  • Binary Notation: starts with zero, followed by a lowercase or upper-case Latin letter B (0b or 0B)
  • Octal notation: starts with 0. If the number after 0 is not in the range from 0 to 7, the number will be converted to a decimal number.
  • The octal syntax is not allowed in the strict mode of ECMAScript 5 and will be treated as decimal.
  • To use the octal number in ECMAScript 6, you need to add the prefix "0o" to a number"
  • Hexadecimal notation: starts with zero, followed by a lowercase or upper-case Latin letter X (0x or 0X)
// Decimal 12345678420777 // it will be treated as octal decimal 511 in non-strict format (in decimal format) // binary var a = 0b100000; // 32var B = 0b0111111; // 63var c = 0B0000111; // 7 // octal var n = 0755; // 493var m = 0644; // 420var a = 0o10; // ES6: octal // hexadecimal 0 xfffffffffffffff // 2951479051793528300000x123456789ABCDEF // 819855292164869000XA/10
Conversion of different hexadecimal values

The hexadecimal conversion mainly uses the toString () method of Number or the parseInt () method:

  • The toString () method accepts a value of 2 ~ The integer parameter between 36 is in decimal format. The default value is decimal, and the Number is converted to String.
  • The second parseInt () parameter accepts a value of 2 ~ The integer parameter between 36 is in decimal format. The default value is decimal. Convert String to Number.
// ToString conversion. The input is Number, and the return value is Stringvar n = 120; n. toString (); // "120" n. toString (2); // "1111000" n. toString (8); // "170" n. toString (16); // "78" n. toString (20); // "60" 0x11. toString (); // "17" 0b111. toString (); // "7" 0x11. toString (12); // "15" // parseInt conversion. If the input is String, the return value is NumberparseInt ('123'); // 110 parseInt ('123', 2 ); // 6 parseInt ('20140901', 8); // 72 parseInt ('20160901', 16); // 110 parseInt ('20160901', 26 ); // 702 // toString and parseInt can be used in combination to convert a from 36 to 12 in hexadecimal mode var a = 'a '; // number parseInt (a, 36) in hexadecimal notation ). toString (12); // "960"
Number object Number

A Number object is a built-in numeric object that encapsulates a series of Number-related constants and methods.

Var na = Number (8); // 8na = Number ('9. 5'); // 9.5na = new Number ('9. 3 '); // Number object, only the prototype method can be used
Number object attributes:

Attribute Description
Number. MAX_VALUE The maximum value that can be expressed.
Number. MIN_VALUE Minimum value that can be expressed
Number. NaN It refers to "non-digit"
Number. NEGATIVE_INFINITY This parameter is used to specify "negative infinity". It is returned when overflow occurs.
Number. POSITIVE_INFINITY It refers to "positive infinity". Return when overflow occurs.
Number. EPSILON Indicates the difference between 1 and the minimum Number that is closer to 1 and greater than 1.
Number. MIN_SAFE_INTEGER Minimum JavaScript Security integer
Number. MAX_SAFE_INTEGER JavaScript maximum security integer

Number object Method

The Number object method can be called in the form of Number. Or global call.

Method Description
Number. parseFloat () Parses string parameters into floating-point numbers, which are equivalent to the one-dimensional operation +
Number. parseInt () Parses a string into an integer corresponding to a specific base number.
Number. isFinite () Determines whether the passed value is a finite number.
Number. isInteger () Determines whether the passed value is an integer.
Number. isNaN () Determine whether the passed value is NaN
Number. isSafeInteger () Determines whether the passed value is a safe integer.

Note the following before using parseInt:

  • The parseInt () parameter can have two parameters: numeric string and hexadecimal
  • If the first parameter is an unspecified numeric string, the result is 0.
  • If the first parameter is not a string, the toString () method of the parameter is called to convert it to a string.
  • If the first parameter does not specify a decimal identifiable character, it will be ignored.
  • If the given string does not have a numeric value, the function returns a special value NaN.
  • If the second parameter is not provided during the call, the number represented by the string is in hexadecimal format.
ParseInt ('20140901'); // 123 parseInt ('20160901', 10); // 123 parseInt ('20160901', 8); // 83 parseInt ('20160901 ', 16); // 291 parseInt ("11", 2); // 3 parseInt ('0x123 ', 10); // 0 parseInt ('0x123', 16 ); // 291 parseInt ('0x123 '); // 291 // if the first parameter is not a string, the first parameter is first converted to the string parseInt ('12', 16 ); // 18 parseInt (12, 16); // 18 // The toString method converts the number to a decimal string parseInt (0xf, 16); // 210xf. toString (); // '15' parseInt ('15', 16); // 21 parseInt ('23. 56 '); // 23 parseInt ("hello", 16); // NaNparseInt ("aello", 16); // 174
Number Type Prototype Method

There are also some methods on the Number type prototype to choose the numerical progress. These methods can be called by the Number instance object:

Method Description
ToExponential () Returns an exponential string of numbers.
ToFixed () Returns the representation of the specified number of decimal places.
ToPrecision () Returns a number with a specified precision.

These prototype methods can be called by Number instance objects:

Var numObj = 12345.6789; numObj. toExponential (); // "1.23456789e + 4" numObj. toExponential (2); // "1.23e + 4" numObj. toExponential (4); // "1.220.e + 4" numObj. toPrecision (); // "12345.6789" numObj. toPrecision (2); // "1.2e + 4" numObj. toPrecision (4); // "1.235e + 4" numObj. toFixed (); // returns "12346": returns a rounded value, excluding the fractional part numObj. toFixed (1); // return "12345.7": Rounding numObj. toFixed (6); // return "12345.678900": Fill with 0 (1.23e + 20 ). toFixed (2); // return "123000000000000000000.00" (1.23e-10 ). toFixed (2); // returns "0.00" 2.34.toFixed (1); // returns "2.3"-2.34.toFixed (1); // returns-2.3 (due to operator priority, A negative number does not return a string) (-2.34 ). toFixed (1); // return "-2.3" (if brackets are used to increase the priority, a string is returned)
Math)

Related to Number, the built-in Math object in JavaScript objects provides many attributes and methods of mathematical constants and functions.

Attribute list:

Attribute Description
Math. E Euler's constant is also the base number of the natural logarithm, approximately 2.718
Math. LN2 The natural logarithm of 2, approximately 0.693
Math. LN10 The natural logarithm of 10, approximately 2.303
Math. LOG2E Base-2 logarithm of E, approximately 1.443
Math. LOG10E Base-10 logarithm of E, approximately 0.434
Math. PI The circumference rate. The ratio of the circumference to the diameter of a circle is approximately 3.14159.
Math. SQRT2 The square root of 2, approximately 1.414
Math. SQRT1_2 The square root of 1/2, approximately 0.707

The sin and other parameters of the trigonometric function in Math are radians, and can be used for angle (Math. PI/180)

Method Description
Math. abs (x) Returns the absolute value of x.
Math. sign (x) Returns the signed function of x to determine whether x is a positive number, negative number, or 0.
Math. random () Returns a pseudo-random number between 0 and 1.
Math. floor (x) Returns x rounded up.
Math. ceil (x) Returns x rounded up.
Math. round (x) Returns the rounded integer.
Math. fround (x) Returns the nearest Single-precision floating point representation of a number.
Math. trunc (x) Returns the integer part of x, removing decimals.
Math. sqrt (x) Returns the square root of x.
Math. cbrt (x) Returns the cube root of x.
Math. hypot ([x [, y [,…]) Returns the square root of the sum of squares of a parameter.
Math. pow (x, y) Returns the Power y of x.
Min (), max () Returns a smaller or more distinct value in the Number Parameter List separated by commas)
Sin (), cos (), tan () Standard trigonometric function; parameter: radian
Asin (), acos (), atan (), atan2 () Returns radians.
Sinh (), cosh (), tanh () Returns radians.
Asinh (), ACO (), atanh () Returns radians.
Pow (), exp (), expm1 (), log10 (), log1p (), log2 () Exponent and logarithm Functions

The above is all the content about the JS numeric Number type knowledge point compiled in this article. Thank you for your support for the help house.

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.