Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert characters into numbers in javascript

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Javascript how to convert characters into numbers, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.

In javascript, the method of converting characters into numbers: 1. Use the "parseInt(value)" statement;2. Use the "parseFloat(value)" statement;3. Use the multiplication operator, syntax "value * 1";4. Use the "Number(value)" statement.

Operating environment of this tutorial: Windows 7 system, Javascript version 1.8.5, Dell G3 computer.

ParseInt()

parseInt() is a global method that converts values to integers. The conversion process is as follows:

Parse the character at position 0 first. If it is not a significant number, NaN is returned directly.

If the character at position 0 is a number or can be converted to a significant digit, continue parsing the character at position 1, if not a significant digit, return the significant digit at position 0 directly.

And so on, analyzing each character one by one from left to right until a non-numeric character is found.

parseInt() will convert all the valid numeric characters analyzed previously to numeric values and return them.

console.log (parseInt("123abc")); //Returns the number 123console.log(parseInt("1.73")); //Returns the number 1console.log(parseInt(".123")); //Return value NaN

Points in floating-point numbers are illegal characters for parseInt(), so fractional values are not converted.

Use parseFloat() function

parseFloat() is also a global method that converts a value to a floating point, i.e. 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")); //Returns the value 1.234

parseFloat() arguments must be decimal strings, not octal or hexadecimal numeric strings. Also, zeros preceding digits (octal digit identifiers) are ignored, and zeros are returned for hexadecimal digits.

console.log (parseFloat("123")); //returns value 123console.log(parseFloat("123abc")); //returns value 123console.log(parseFloat ("010")); //returns value 10console.log(parseFloat("0x10")); //returns value 0console.log(parseFloat("x10")); //returns value NaN

3. Use the multiplication operator

If a variable is multiplied by 1, the variable is automatically converted to a numeric value by JavaScript. After multiplying by 1, the result does not change, but the type of value is converted to a numeric value. If the value cannot be smoothed to a legal value, NaN is returned.

var a = 1; //numeric var b = "1"; //numeric string console.log (a + (b * 1)); //returns numeric 2

Use the Number() function

The Number() cast is handled differently than the parseInt() and parseFloat() methods, in that Number() converts the whole value, not the local value.

console.log (Number("123abc")); //returns NaNconsole.log (Number("123")); //returns the value 123 Is it helpful to read the above? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report