Home > Article > Web Front-end > js parsefloat parseint conversion function_basic knowledge
js parsefloat parseint
JavaScript provides three explicit type conversion functions, namely eval(), parseInt() and parseFloat().
eval() function: Convert a string expression into a numeric value. For example, the result of the statement total=eval("432.1*10") is total=4321, that is, the value 4321 is assigned to the total variable.
parseInt() function: Convert string to integer. The return starts from the first character of the string, or 0 if the character does not start with an integer. 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 parseInt() function, it returns the first floating point number contained on the string. If the string does not start with a valid floating point number, 0 is returned.
For example: parseFloat("2.1e4xyz") returns 21000; and parseFloat("xyz") returns 0.
The parseFloat function method in JavaScript returns a floating point number converted from a string. Usage:
parseFloat(numString) where the numString parameter is a string containing a floating point number. The parseFloat function method in JavaScript returns a numerical representation equal to the number stored in numString. If the prefix of numString cannot be interpreted as a floating point number, NaN is returned (instead of a number).
parseFloat("abc") // Returns NaN.
parseFloat("1.2abc") // Returns 1.2.