SoFunction
Updated on 2025-03-01

Detailed explanation of JS conversion numerical functions Number, parseInt, parseFloat

There are three functions in JS that can convert non-numeric values ​​into numeric values: Number(), parseInt() and parseFloat(). The first function, namely the transformation function Number(), can be used for any data type, while the other two functions are specifically used to convert strings into numeric values. These 3 functions will return different results for the same input.

Number()

The conversion rules of Number() function are as follows:

•If the parameter is a Date object, return the number of milliseconds from January 1, 1970 to the present.
• If it is a Boolean value, true and false will be replaced by 1 and 0 respectively.
•If it is a numeric value, it is just a simple pass and return
•If it is a null value, return 0
•If undefined, return NaN
•If it is a string, follow the following rules: •If the string contains only numbers (including the hexadecimal format "0x"), convert it to the corresponding decimal.
• If the string is empty, return 0.
• If the string contains other characters, return NaN.

•If it is an object, call the object's valueOf() method, and then convert the returned value according to the previous rules. If the result of the conversion is NaN, the toString() method of the object is called, and then the returned string value is converted in turn according to the previous rules.

(Number(true)+"<br/>"); //Return 1(Number(false)+"<br/>"); //Return 0(Number(null)+"<br/>"); //Return 0(Number(undefined)+"<br/>"); //Return to NaN(Number("001")+"<br/>"); //Return 1(Number("001.1")+"<br/>"); //Return 1.1(Number("0x10")+"<br/>"); //Return to 16(Number(new String("11aa"))+"<br/>"); //returnNaN

parseInt()

Returns the integer converted from the string.

parseInt(numString, [radix])

parameter

numString is mandatory. A string to convert to a number. radix optional. The value of the binary value of the number saved by numString between 2 and 36 represents the number of numString. If not provided, a string prefixed with '0x' is treated as hexadecimal and a string prefixed with '0' is treated as octal. All other strings are considered decimal.

illustrate

The parseInt() method returns an integer equal to the numeric value stored in numString. If the prefix of numString cannot be interpreted as an integer, NaN is returned (not a number). It ignores the spaces in front of the string until the first non-space character is found.

Example

(parseInt("1")+"<br/>"); //Return 1(parseInt("  1")+"<br/>"); //Return 1(parseInt("1.1")+"<br/>"); //Return 1(parseInt("11aa")+"<br/>"); //Return to 11(parseInt("020")+"<br/>"); //Return 16 (octal)(parseInt("")+"<br/>"); //Return to NaN(parseInt("AF")+"<br/>"); //Return to NaN(parseInt("AF",16)+"<br/>"); //Return to 175(parseInt("0xAF")+"<br/>"); //return175

parseFloat()

Returns the floating point number obtained by the string conversion.

parseFloat(numString)

Required numString parameter is a string containing floating point numbers.

illustrate

parseFloat() returns a numeric representation equal to the number saved in numString. If the prefix of numString cannot be interpreted as a floating point number, NaN is returned (not a number).

Summarize

The above is the JS conversion numerical functions Number(), parseInt(), and parseFloat() introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!