parseint () Function: Converts a string to an integer. Returns the start of the first character of the string and returns 0 if the character does not begin with an integer. The parseint () function can also convert hexadecimal or decimal digits.
For example: parseint ("123xyz") returns 123, and parseint ("xyz") returns 0.
Parsefloat () function: Similar to the parseint () function, which returns the first floating-point number contained on a string. Returns 0 if the string does not start with a valid floating-point number.
For example: parsefloat ("2.1e4xyz") returns 21000, while parsefloat ("xyz") returns 0.
The Parsefloat function method in JavaScript is to return a floating-point number converted from a string
Look at 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 to strings
1. Extract the integer in the string: parseint (); Example: The result of parseint ("123zhang") is 123
2. Extract the floating-point number in the string: parsefloat (); Example: The result of parsefloat ("0.55zhang") is 0.55
For example, if you want to convert the string "1234blue" to an integer, then parseint () will return 1234, because when it detects character B, it stops the detection process. The literal number contained in the string is converted to numbers correctly, so the string "0xA" is correctly converted to Number 10. However, the string "22.5" will be converted to 22 because the decimal point is an invalid character for an integer. Some examples are as follows:
parseint ("1234blue"); Returns 1234
parseint ("0xA"); Returns 10
parseint ("22.5"); Returns 22
parseint ("Blue"); Returns NaN
The parseint () method also has a base pattern that converts binary, octal, hexadecimal, or any other string of strings into integers. The base is specified by the second parameter of the parseint () method, so to parse the hexadecimal value, you 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:
parseint ("10", 2); Returns 2
parseint ("10", 8); Returns 8
parseint ("10", 10); Returns 10
If the decimal number contains a leading 0, it is best to use cardinality 10 so that you do not accidentally get the octal value. 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, two lines of code parse the string "010" into a number. The first line of code regards this string as a octal value, parsing it in the same way as the second line of code (the Declaration cardinality is 8). The last line of code declares a cardinality of 10, so the iNum3 finally equals 10.
The following is an example of using the Parsefloat () method:
The code is as follows |
Copy Code |
Parsefloat ("1234blue"); 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
|
Summarize
parseint () and parsefloat () Two methods are all found from the left string, if the first character is not a number or a minus sign (in parsefloat () it can also be a decimal point). Once they have encountered such a character, they return the number they have extracted