Differences between parseInt and parseFloat types in javascript

Source: Internet
Author: User

In js, parseInt and parseFloat both define or convert the numeric type, but one is an integer that supports the floating point type, that is, they have different precision.

ParseInt () function: converts a string to an integer. The return value starts with the first character of the string. If the character does not start with an integer, 0 is returned. The parseInt () function can also convert hexadecimal or decimal numbers.

For example, parseInt ("123xyz") returns 123, while parseInt ("xyz") returns 0.

ParseFloat () function: similar to the parseInt () function, it returns the first floating point number contained in the string. If the string does not start with a valid floating point number, 0 is returned.

For example, parseFloat ("2.1e4xyz") returns 21000, while parseFloat ("xyz") returns 0.

In JavaScript, The parseFloat function returns the floating point number obtained by String Conversion.

Check the Code:

<Script>
Alert (parseInt ("3.54 apples "));
Alert (parseFloat ("3.54 apples "));
</Script>
<Script>
Alert (parseInt ("3.54 apples "));
Alert (parseFloat ("3.54 apples "));
</Script>

 

They can also be used to convert numbers into strings.


1. Extract the integer parseInt () in the string. For example, the result of parseInt ("123 zhang") is 123.
2. Extract the floating point: parseFloat () in the string. For example, the result of parseFloat ("0.55 zhang") is 0.55.


For example, if you want to convert the string "1234 blue" to an integer, parseInt () returns 1234 because the detection process stops when it detects character B. The number literal in the string is correctly converted to a number, so the string "0xA" is correctly converted to a number 10. However, the string "22.5" is converted to 22 because the decimal point is invalid for integers. Some examples are as follows:

ParseInt ("1234 blue"); // returns 1234
ParseInt ("0xA"); // returns 10
ParseInt ("22.5"); // returns 22
ParseInt ("blue"); // returns NaN

The parseInt () method also has the base mode, which can convert binary, octal, hexadecimal, or any other hexadecimal string to an integer. The base is specified by the second parameter of the parseInt () method. to parse the hexadecimal value, you need to call the parseInt () method as follows:
ParseInt ("AF", 16); // returns 175
Of course, for binary, octal, or even decimal (default mode), you can call the parseInt () method as follows:

ParseInt ("10", 2); // returns 2
ParseInt ("10", 8); // returns 8
ParseInt ("10", 10); // returns 10

If the decimal number contains the leading 0, it is best to use base 10 to avoid unexpected octal values. For example:

The Code is as follows: Copy code

ParseInt ("010"); // returns 8
ParseInt ("010", 8); // returns 8
ParseInt ("010", 10); // returns 10

In this Code, both lines of code parse the string "010" into a number. The first line of code regards this string as the octal value. The method for parsing this string is the same as that of the second line of code (The Declaration base is 8. The last line of Code declares that the base number is 10, so iNum3 is equal to 10 at last.

The following is an example of using parseFloat:

The Code is as follows: Copy code


ParseFloat ("1234 blue"); // returns 1234.0
ParseFloat ("0xA"); // returns NaN
ParseFloat ("22.5"); // returns 22.5
ParseFloat ("22.34.5"); // returns 22.34
ParseFloat ("0908"); // returns 908
ParseFloat ("blue"); // returns NaN

Summary

The parseInt () and parseFloat () Methods start from the left string. If the first character is not a number or a negative number (in parseFloat (), it can also be a decimal point ). Once they encounter such a character, they return their own extracted numbers.

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.