Conversion method: 1. Use operators such as "-", "*", "/", "%", " ", "--"; 2. Use the "Number (value)" statement; 3. Use the "parseInt(stringNum)" statement; 4. Use the "parseFloat(stringNum)" statement.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
javascript converts a string into a number
Method 1: Use -
, *
, /
, %
,
, -- and other operators
JavaScript will automatically convert strings into numbers , if it cannot be converted to a number, it is converted to NaN. For example:
alert("30"/5); //除运算,结果为:6 alert("15"-5); //减运算,结果为:10 alert("20"*"a"); //乘运算,结果为:NaN alert("20"%"3"); //取模运算,结果为:2
Method 2: Use the Number() function
The Number() function can convert the parameter into a number
The format is as follows:
var num1 = "6"; var num2 = "6"; var num3 = "a"; alert(++num1); //将字符串转换为数字再进行++运算,结果为:7 alert(--num2); //将字符串转换为数字再进行--运算,结果为:5 alert(++num3); //字符串无法转换为数字,结果为:NaN
Number() performs an overall conversion of the parameter value. When the parameter value contains symbols that cannot be converted to numbers anywhere, the conversion fails and NaN will be returned. Otherwise, it will be returned. The converted number.
Number() follows the following rules when converting parameters to numbers:
If the parameter only contains numbers, it will be converted to a decimal number, ignoring the leading 0 and leading spaces; if the number is preceded by -, - will be retained in the conversion result; if the number is preceded by, the sign will be deleted after conversion;
If the parameter contains valid floating point numbers , will be converted into the corresponding floating point number, ignoring leading 0 and leading spaces; if the number is preceded by -, - will be retained in the conversion result; if the number is preceded by, the sign will be deleted after conversion;
If the parameter contains a valid hexadecimal number, it will be converted to a decimal number of the corresponding size;
If the parameter is an empty string, it will be converted to 0;
If the parameter is a Boolean value, true will be converted to 1 and false will be converted to 0;
If the parameter is null, it will be converted is 0;
If the parameter is undefined, it will be converted to NaN;
If the parameter is a Date object, it will be converted to 1 from 1970 The number of milliseconds from the 1st of the month to when the conversion is performed;
If the parameter is a function, an array object containing more than two elements, and other objects except Date objects, it will be converted to NaN;
If the parameter contains other special symbols or non-numeric characters except spaces, and - in front of the parameter, or the parameter contains special symbols or non-numeric characters including spaces, and - in the middle of the parameter character, will be converted to NaN.
Conversion example:
Number(value)
From the above example, we can also see that Number() is converted as a whole. Any illegal characters contained in any place will cause the conversion to fail. success. The difference between the two functions introduced next and Number() is that the conversion is performed bit by bit from left to right. When any bit cannot be converted, the conversion stops immediately and the successfully converted value is returned.
Method 3: Use the parseInt() function
The parseInt() function can convert the parameter into an integer
The format is as follows:
Thealert(Number("0010")); //去掉两个前导0,结果为:10 alert(Number("+010")); //去掉前导0和+,结果为:10 alert(Number("-10")); //转换后保留“-”号,结果为:-10 alert(Number('')); //空字符串的转换结果为:0 alert(Number(true)); //布尔值true的转换结果为:1 alert(Number(null)); //null值的转换结果为:0 var d = new Date(); //创建一个Date对象 alert(Number(d)); //转换Date对象,结果为1970.1.1至执行转换时的毫秒数:1511351635179 alert(Number("100px")); //参数中包含了不能转换为数字的字符px,结果为:NaN alert(Number("100 01")); //参数中包含了空格,导致整个参数不能转换,结果为:NaN alert(Number("100-123")); //参数中包含了“-”,导致整个参数不能转换,结果为:NaN var a; //声明变量 alert(Number(a)); //变量a没有赋值,因而a的值为undefined,转换undefined的结果为:NaN var fn = function (){alert(1);}; //创建一个函数对象 alert(Number(fn)); //转换函数,结果为:NaN alert(Number(window)); //转换window对象,结果为:NaN
stringNum parameter is a string that needs to be converted into an integer; the radix parameter is a number between 2 and 36, indicating the base number of the stringNum parameter. It can be omitted when the value is 10.
The function of parseInt() is to parse the stringNum string parameter with radix as the base into a decimal number. If the stringNum string does not start with a legal character, NaN will be returned; if an illegal character is encountered during the parsing process, parsing will stop immediately and the parsed value will be returned.
parseInt() follows the following rules when parsing a string as an integer:
When parsing a string, spaces before and after the string will be ignored; if is -, - will be retained in the conversion result; if the number is preceded by, the sign will be deleted after conversion;
If the string is preceded by special symbols other than spaces, and - or For non-numeric characters other than a~f (or A~F), the string will not be parsed, and the returned result is NaN;
contains spaces, , - and decimal point "." and other special symbols or non-numeric characters, the parsing will stop when these characters are encountered and the parsed result will be returned;
If the string is Empty string, the return result is NaN.
Conversion example:
parseInt(stringNum,[radix])
From the above example we can see that when parseInt() parses floating point numbers, the decimal part of the data will be truncated, and you need to use ParseFloat(), which will be introduced below, cannot be used instead of parseInt().
Method 4: Use the parseFloat() function
The parseFloat() function can convert the parameter into a floating point number
The format is as follows:
alert(parseInt("1101",2)); //以2为基数的1101字符串解析后的结果为:13 alert(parseInt("a37f",16)); //以16为基数的a37f字符串解析后的结果为:41855 alert(parseInt("123")); //以10为基数的123字符串解析后的结果为:123 alert(parseInt(" 123")); //字符串前面的空格会被忽略,结果为:123 alert(parseInt("12 3")); //字符串中包含了空格,解析到空格时停止,结果为12 alert(parseInt("12.345")); //字符串中包含了小数点,解析到小数点时停止,结果为12 alert(parseInt("xy123")); //字符串前面包含了非数字字符“x”,无法解析,返回结果为:NaN alert(parseInt("123xy4")); //字符串中包含了非数字字符“xy”,解析到“x”时停止,结果为:123
stringNum parameter is a string that needs to be parsed into a floating point type.
parseFloat() 的作用是将首位为数字的字符串转解析成浮点型数。若 stringNum 字符串不是以合法的字符开头,则返回 NaN;解析过程中如果遇到不合法的字符,将马上停止解析,并返回已经解析的值。
parseFloat() 在解析字符串为整数时,遵循以下规则:
解析字符串时,会忽略字符串前后的空格;如果字符串前面为-,-会保留在转换结果中;如果数字前面为+,转换后将删掉+号;如果字符串前面为小数点.转换结果会在小数点前面添加 0;
如果字符串前面为除空格、+、-和。以外的特殊符号,字符串将不会被解析,返回结果为 NaN;
在字符串中包含了空格、+和-等特殊符号或非数字的字符时,解析将在遇到这些字符时停止,并返回已解析的结果;
在字符串中包含两个以上为小数点时,解析到第二个小数点时将停止解析,并返回已解析的结果;
如果字符串是空字符串,返回结果为 NaN。
转换示例:
alert(parseFloat("312.456"));//结果为:312.456 alert(parseFloat("-3.12"));//字符串前面的“-”将保留,结果为:-3.12 alert(parseFloat("+3.12"));//字符串前面的“-”将保留,结果为:3.12 alert(parseFloat(".12"));//在小数点前面添加0,结果为:0.12 alert(parseFloat(" 3.12"));//截掉字符串前面的空格,结果为:3.12 alert(parseFloat("312.4A56"));//字符串中包含非数字字符A,解析到A时停止,结果为:312.4 alert(parseFloat("31 2.4A56"));//字符串中包含空格,解析到空格时停止,结果为:31 alert(parseFloat("31.2.5"));//字符串中包含两个小数点,解析到第二个小数点时停止,结果为:31.2 alert(parseFloat("a312.456"));//字符串前面为非数字字符a,解析无法进行,结果为:NaN
【推荐学习:javascript高级教程】
The above is the detailed content of How to convert string to number in javascript. For more information, please follow other related articles on the PHP Chinese website!