js-Numeric conversions

Source: Internet
Author: User

JavaScript to deal with very numeric values

Number (): Any data type converted to numeric value;

parseint (), parsefloat (): Converts a string into a numeric value;

Conversion rules:

Number ():

1) If it is a Boolean value, True and false are converted to 1 and 0, respectively.

2) If it is a numeric value, it is simply passed in and returned.

3) If it is a null value, return 0.

4) If it is undefined, return nan.

5) If it is a string, follow these rules:

If the string contains only numbers (including the case with a plus or minus sign), it is converted to a decimal value, that is, "1" becomes 1, "123" becomes 123, and "011" becomes 11 (leading 0 is ignored);
If the string contains a valid floating-point format, such as "1.1", it is converted to the corresponding floating-point value (also ignoring the leading 0);
If the string contains a valid hexadecimal format, such as "0xf", the other is converted to a decimal integer value of the same size;
If the string is empty (contains no characters), it is converted to 0;
If the string contains characters other than the above format, the other is converted to Nan.
6) If it is an object, call the ValueOf () method of the object and then convert the returned value according to the previous rule. If the result of the conversion is Nan, the object's ToString () method is called, and then the returned string value is converted again according to the previous rule.

Ex

var num1=number ("Hello World"); NaN

var num2=number (""); 0

var num3=number ("000011"); 11

var num4=number (true); 1

Because the number () function is complex and unreasonable when converting strings, the parseint () function is more commonly used when working with integers.

parseint ():

When converting a string, it is more time to see whether it conforms to the numeric pattern. Whitespace before the string is ignored until the first non-whitespace character is found.

If the first character is not a numeric character or both minus, parseint () returns Nan; In other words, converting an empty string with parseint () returns Nan.
If the first character is a numeric character, parseint () continues to parse the second character until all subsequent characters have been parsed or a non-numeric character is encountered. For example, "1234blue" is converted to 1234 because "blue" is completely ignored. Similarly, "22.5" is converted to 22 because the decimal point is not a valid numeric character.
If the string starts with "0x" and is followed by a numeric character, it is treated as a hexadecimal integer;
If the string starts with "0" and is followed by a numeric character, it is treated as an octal integer;
The parseint () function adds a second parameter to specify the cardinality (that is, how much) to use when converting, such as: parseint ("10", 16)//hexadecimal parsing, parseint ("10", 8)//by octal
Parsefloat ():

Similar to the parseint () function, parsefloat () is also parsed every character from the first character (position 0), and is parsed to the end of the string, or to an invalid floating-point numeric character. That is, the first decimal point in the string is valid, and the second decimal point is invalid, so the string following it is ignored. For example: "22.34.5" will be converted to 22.34.

Except for the first decimal point, the second difference between parsefloat () and parseint () is that it will always ignore the leading 0. Parsefloat () can recognize all floating-point numeric formats discussed earlier, and also include decimal integer formats. But the hexadecimal-formatted string is always converted to 0. Because Parsefloat () parses only the decimal value, it does not specify the usage of the cardinality with the second argument.

In addition, if the string contains a number that can be resolved to an integer (no decimal point, or 0 after the decimal point), parsefloat () returns an integer.

Ex

var num1=parsefloat ("1234blue"); 1234

var num2=parsefloat ("0xA"); 0

var num3=parsefloat ("0908.5"); 908.5

var num4=parsefloat ("3.125e7"); 31250000

Attention:

1) It is worth noting that the highest precision of a floating-point number is 17 decimal places, but its accuracy is much inferior to integers in arithmetic calculations. For example, the result of 0.1 plus 0.2 is not 0.3, but the result of 0.30000000000000004;99.99 plus 0.1 is not 100.09 but 100.08999999999999. This little bit of error will result in the inability to test specific floating-point values.

For example:

if (a+b==0.3)//Do not do such a test

2) It is possible to calculate the result of Nan at the time of calculation, ECMAScript defines the isNaN () function. This function takes a parameter, which can be any type, and the function will help us determine whether the parameter is "not a value". IsNaN () after receiving a value, it attempts to convert the value to a number. Parameters that cannot be converted to numeric values return true.

js-Numeric conversions

Related Article

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.