如何在JavaScript中将字符串转换为整数?
当前回答
这(可能)不是解析整数的最佳解决方案,但如果您需要“提取”一个整数,例如:
"1a2b3c" === 123
"198some text2hello world!30" === 198230
// ...
这将起作用(仅适用于整数):
var str='3a9b0c3d2e9f8g'函数提取器整数(str){var结果=0;无功因数=1对于(var i=str.length;i>0;i-){if(!isNaN(str[i-1])){result+=parseInt(str[i-1])*因子系数*=10}}返回结果}console.log(extractInteger(str))
当然,这也适用于解析整数,但比其他方法慢。
您也可以使用此方法解析整数,如果字符串不是数字,则返回NaN,但我不明白为什么要这样做,因为这在内部依赖于parseInt,parseInt可能更快。
var str='3a9b0c3d2e9f8g'函数提取器整数(str){var结果=0;无功因数=1对于(var i=str.length;i>0;i-){if(isNaN(str[i-1]))返回NaNresult+=parseInt(str[i-1])*因子系数*=10}返回结果}console.log(extractInteger(str))
其他回答
将数字的乘法与各自的十次幂相加:
即:123=100+20+3=1100+2+10+31=1*(10^2)+2*(10^1)+3*(10^0)
function atoi(array) {
// Use exp as (length - i), other option would be
// to reverse the array.
// Multiply a[i] * 10^(exp) and sum
let sum = 0;
for (let i = 0; i < array.length; i++) {
let exp = array.length - (i+1);
let value = array[i] * Math.pow(10, exp);
sum += value;
}
return sum;
}
尝试parseInt。
var number = parseInt("10", 10); //number will have value of 10.
JavaScript中有许多方法可以将字符串转换为数值。。。所有这些都简单方便。选择适合您的方式:
var num = Number("999.5"); //999.5
var num = parseInt("999.5", 10); //999
var num = parseFloat("999.5"); //999.5
var num = +"999.5"; //999.5
此外,任何数学运算都会将它们转换为数字,例如。。。
var num = "999.5" / 1; //999.5
var num = "999.5" * 1; //999.5
var num = "999.5" - 1 + 1; //999.5
var num = "999.5" - 0; //999.5
var num = Math.floor("999.5"); //999
var num = ~~"999.5"; //999
我更喜欢的方式是使用+符号,这是在JavaScript中将字符串转换为数字的优雅方式。
ParseInt()和+不同
parseInt("10.3456") // returns 10
+"10.3456" // returns 10.3456
JavaScript中的字符串到数字:
一元+(最推荐)
+numStr易于使用,性能优于其他支持整数和小数
console.log(+'123.45') // => 123.45
其他一些选项:
正在分析字符串:
整数的parseInt(numStr)整数和小数的parseFloat(numStr)
console.log(parseInt('123.456')) // => 123
console.log(parseFloat('123')) // => 123
JavaScript函数
整数的数学函数,如round(numStr)、floor(numStr)、ceil(numStr)整数和小数的数字(numStr)
console.log(Math.floor('123')) // => 123
console.log(Math.round('123.456')) // => 123
console.log(Math.ceil('123.454')) // => 124
console.log(Number('123.123')) // => 123.123
单目运算符
所有基本一元运算符,+numStr、numStr-0、1*numStr,numStr*1和numStr/1所有都支持整数和小数注意numStr+0。它返回一个字符串。
console.log(+'123') // => 123
console.log('002'-0) // => 2
console.log(1*'5') // => 5
console.log('7.7'*1) // => 7.7
console.log(3.3/1) // =>3.3
console.log('123.123'+0, typeof ('123.123' + 0)) // => 123.1230 string
位运算符
两个波浪号~~numStr或左移0,numStr<<0只支持整数,不支持小数
console.log(~~'123') // => 123
console.log('0123'<<0) // => 123
console.log(~~'123.123') // => 123
console.log('123.123'<<0) // => 123
//正在分析console.log(parseInt('123.456'))//=>123console.log(parseFloat('123'))//=>123//功能console.log(数学楼层('123'))//=>123console.log(数学舍入('123.456'))//=>123console.log(数学.ceil('123.454'))//=>124console.log(编号('123.123'))//=>123.123//Unary公司console.log(+'123')//=>123console.log('002'-0)//=>2console.log(1*'5')//=>5console.log('7.7'*1)//=>7.7console.log(3.3/1)//=>3.3console.log('123.123'+0,typeof('123.123'+0))//=>123.1230字符串//按位(Bitwise)console.log(~~'123')//=>123console.log('0123'<<0)//=>123console.log(~~'123.123')//=>123console.log('123.123'<<0)//=>123
推荐文章
- 将RGB转换为白色的RGBA
- 如何将“camelCase”转换为“Camel Case”?
- 我们可以在另一个JS文件中调用用一个JavaScript编写的函数吗?
- 如何使用JavaScript重新加载ReCaptcha ?
- jQuery。由于转义了JSON中的单引号,parseJSON抛出“无效JSON”错误
- 如何获得一个变量值,如果变量名存储为字符串?
- 在Ruby中不创建新字符串而修饰字符串的规范方法是什么?
- 为什么不是字符串。空一个常数?
- 在JavaScript关联数组中动态创建键
- ReactJS和公共文件夹中的图像
- 在React Native中使用Fetch授权头
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?