In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "how to convert values to numeric types in javascript". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to convert values to numerical types in javascript.
Conversion to the numerical type of the method: 1, the use of parseInt () function, you can convert the value to an integer, syntax "parseInt (value)"; 2, the use of parseFloat () function, you can convert the value to a floating point number, syntax "parseFloat (value)"; 3, the use of multiplication operator, syntax "value * 1".
Javascript's method of converting values to numeric types:
Method 1: use the parseInt () function
ParseInt () is a global method that converts values to integers. 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.
ParseInt () converts all the legal numeric characters from the previous analysis to numeric values and returns them.
Console.log (parseInt ("123abc")); / / return numeric 123console.log (parseInt ("1.73")); / / return numeric 1console.log (parseInt (".123")); / / return value NaN
The point in the floating point is an illegal character for parseInt (), so the value of the decimal part is not converted.
If it is a numeric string that begins with 0, parseInt () treats it as an octal number: it is converted to an octal value and then returned as a decimal number.
If it is a numeric string that begins with 0x, parseInt () treats it as a hexadecimal number: it is converted to a hexadecimal value and then returned as a decimal number.
Var d = 010; / / octal number string var e = 0x10; / / hexadecimal number string console.log (parseInt (d)); / / return decimal number 8console.log (parseInt (e)); / / return decimal number 16
ParseInt () also supports base mode, which converts binary, octal, hexadecimal, and other numeric strings to integers. The base mode is specified by the second argument of the parseInt () function.
Example
The following code converts binary, octal, and decimal number strings to decimal integers.
Console.log (parseInt ("10", 2)); / convert the binary number 10 to a decimal integer, to 2console.log (parseInt ("10", 8)); / / convert the octal number 10 to a decimal integer, to 8console.log ("10", 10); / / convert the decimal number 10 to a decimal integer, to 10
Method 2: use the parseFloat () function
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. The parsing process is the same as the parseInt () method.
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 ("123")); / / 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
Method 3: use the multiplication operator
If the variable is multiplied by 1, the variable is automatically converted to a numeric value by JavaScript. After multiplying 1, the result does not change, but the type of the value is converted to a numeric value. Returns NaN if the value cannot be cached to a legal value.
Var a = 1; / numeric var b = "1"; / / numeric string console.log (a + (b * 1)); / / return numeric 2 so far, I believe you have a deeper understanding of "how to convert values into numeric types in javascript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.