Javascript number type

Source: Internet
Author: User

I. Numerical Conversion

Number (), parseint (), and parsefloat ()

1. Number () function conversion rules: applicable to any data type.

Boolean values true and false are converted to 1 and 0 respectively;

The value is just a simple graph transfer and return;

Convert null to 0;

Undefined to Nan;

Conversion rules when the target is a string:

A string contains only numbers and is converted to a decimal value. Leading 0 is ignored (including octal values, because octal values are composed of leading 0 and other numbers ranging from 0 to 7 );

The string contains valid floating point values and is converted to the corresponding floating point number;

The string contains valid hexadecimal numbers, which are converted to equal decimal values;

Convert an empty string to 0;

2. parseint () Conversion Function, dedicated to strings

Conversion rules:

This conversion method is mainly used to convert a string. parseint returns Nan if the first character of the result is not a numeric character or a negative sign.

Parseint () can accept two parameters. The first value indicates the value to be converted, and the second parameter indicates the conversion method (in decimal, hexadecimal, or octal format ). If there is no second parameter, it is converted to decimal by default.

3. parsefloat () conversion method. This method always ignores the leading 0 and only one decimal point is valid. if there is only a number 0 after the decimal point, an integer is returned.

Ii. Other methods of Number Type

Tofixed () is automatically rounded to the nearest integer. A parameter is used to indicate the number of decimal places returned.

Toexponential () returns a string in exponential notation.

Toprecision (), automatically select the appropriate representation method.

Note: all three methods return strings.

Iii. Others

Isnan () can determine whether the given value is a numerical value. This function converts the passed value and determines whether it can convert digits.

Isfinite () can be used to test whether the value is within the data range supported by the browser.

The highest precision of floating point numbers is 17 decimal places. Do not test (0.1 + 0.2 = 0.3)

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.