How javascript converts values to floating-point numbers
This article introduces the relevant knowledge of "how javascript converts values into floating-point numbers". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Javascript converts values to floating-point numbers
In javascript, you can use the parseFloat () method to convert a value to a floating-point number, parse a string, and return a floating-point number.
ParseFloat () is also a global method that converts values to floating-point numbers, that is, it recognizes the first occurrence of the decimal point, while the second decimal point is considered illegal.
This function specifies whether the first character in the string is a number. If so, the string is parsed until the end of the number is reached, and then the number is returned as a number, not as a string.
The conversion process is as follows:
Parse the character at position 0 first, and return NaN directly if it is not a valid number.
If the character at position 0 is a number, or can be converted to a valid number, continue to parse the character at position 1, and if it is not a valid number, directly return the valid number at position 0.
And so on, analyze each character one by one from left to right until a non-numeric character is found.
ParseFloat () converts all the legal numeric characters from the previous analysis to numeric values and returns them.
Console.log (parseFloat ("1.234.5")); / / the returned value is 1.234
The argument to parseFloat () must be a string in decimal form, not an octal or hexadecimal numeric string. At the same time, the 0 (octal digit ID) in front of the number is ignored and 0 is returned for hexadecimal numbers.
Console.log (parseFloat ("123abc")); / / return value 123console.log (parseFloat ("123abc")); / / return value 123console.log (parseFloat ("010")); / / return value 10console.log (parseFloat ("0x10")); / / return value 0console.log (parseFloat ("x10")); / / return value NaN "how to convert a value into a floating point number" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!