如何在JavaScript中将字符串转换为整数?
当前回答
在我看来,没有答案涵盖所有边缘情况,因为解析浮点值会导致错误。
function parseInteger(value) {
if(value === '') return NaN;
const number = Number(value);
return Number.isInteger(number) ? number : NaN;
}
parseInteger("4") // 4
parseInteger("5aaa") // NaN
parseInteger("4.33333") // NaN
parseInteger("aaa"); // NaN
其他回答
最快的
var x = "1000"*1;
Test
这里有一些速度比较(仅限macOS)…:)
对于Chrome,“plus”和“mul”是最快的(>700000,00运算/秒),“Math.floor”是最慢的。对于Firefox,“plus”是最慢的(!)“mul”是最快的(>900000000操作/秒)。在Safari中,“parseInt”是最快的,“number”是最慢的(但结果非常相似,>130000<31000000)。因此Safari将字符串转换为int比其他浏览器慢10倍以上。所以获胜者是“mul”:)
您可以通过此链接在浏览器上运行它https://jsperf.com/js-cast-str-to-number/1
我还测试了var x=~~“1000”;。在Chrome和Safari上,它比var x=“1000”*1慢一点(<1%),在Firefox上则快一点(<1%)。
另一种选择是将值与自身进行双重XOR:
var i = 12.34;
console.log('i = ' + i);
console.log('i ⊕ i ⊕ i = ' + (i ^ i ^ i));
这将输出:
i = 12.34
i ⊕ i ⊕ i = 12
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
前面的所有答案都是正确的。请先通过“typeot x==='number'”确认这是字符串中的数字。否则,它将返回NaN。
var num = "fsdfsdf242342";
typeof num => 'string';
var num1 = "12423";
typeof num1 => 'number';
+num1 = > 12423`
最简单的方法是使用本机Number函数:
var x = Number("1000")
如果这对你不起作用,那么有parseInt、一元加号、parseFloat with floor和Math.round方法。
parseInt()
var x = parseInt("1000", 10); // You want to use radix 10
// So you get a decimal number even with a leading 0 and an old browser ([IE8, Firefox 20, Chrome 22 and older][1])
一元加
如果字符串已经是整数形式:
var x = +"1000";
地板()
如果字符串是或可能是浮点数,并且需要整数:
var x = Math.floor("1000.01"); // floor() automatically converts string to number
或者,如果您要多次使用Math.floor:
var floor = Math.floor;
var x = floor("1000.01");
parseFloat()
如果你是一个在调用parseInt时忘记输入基数的类型,你可以使用parseFloat并根据自己的喜好进行舍入。这里我用地板。
var floor = Math.floor;
var x = floor(parseFloat("1000.01"));
舍入()
有趣的是,Math.round(如Math.floor)将进行字符串到数字的转换,因此如果您希望数字四舍五入(或如果字符串中有整数),这是一个很好的方法,可能是我的最爱:
var round = Math.round;
var x = round("1000"); // Equivalent to round("1000", 0)