如何在JavaScript中将字符串转换为整数?


当前回答

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

其他回答

最简单的方法是这样使用+

const strTen = "10"
const numTen = +strTen      // string to number conversion
console.log(typeof strTen)  // string
console.log(typeof numTen)  // number

尝试parseInt。

var number = parseInt("10", 10); //number will have value of 10.

最简单的方法是使用本机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)

在JavaScript中,您可以执行以下操作:

解析Int

parseInt("10.5") // Returns 10

与1相乘

var s = "10";
s = s*1;  // Returns 10

使用一元运算符(+)

var s = "10";
s = +s;  // Returns 10

使用按位运算符

(注:在2140000000之后开始破裂。示例:~~“2150000000”=-214967296)

var s = "10.5";
s = ~~s; // Returns 10

使用Math.floor()或Math.ceil()

var s = "10";
s = Math.floor(s) || Math.ceil(s); // Returns 10

我只在字符串前加了一个加号(+),这就是解决方案!

+"052254" // 52254