如何在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

其他回答

我用这个

String.prototype.toInt = function (returnval) {
    var i = parseInt(this);
     return isNaN(i) ? returnval !== undefined ? returnval : - 1  :      i;
}

var str = "7";
var num = str.toInt(); // outputs 7, if not str outputs -1
//or
var num = str.toInt(0); // outputs 7, if not str outputs 0

这样我总能得到一个整数。

在我看来,没有答案涵盖所有边缘情况,因为解析浮点值会导致错误。

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

尝试parseInt。

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

确保获得有效整数的最安全方法:

let integer = (parseInt(value, 10) || 0);

示例:

// Example 1 - Invalid value:
let value = null;
let integer = (parseInt(value, 10) || 0);
// => integer = 0
// Example 2 - Valid value:
let value = "1230.42";
let integer = (parseInt(value, 10) || 0);
// => integer = 1230
// Example 3 - Invalid value:
let value = () => { return 412 };
let integer = (parseInt(value, 10) || 0);
// => integer = 0

尝试parseInt函数:

var number = parseInt("10");

但有一个问题。如果您尝试使用parseInt函数转换“010”,它将检测为八进制数,并将返回数字8。因此,需要指定基数(从2到36)。在这种情况下,底座10。

parseInt(string, radix)

例子:

var result = parseInt("010", 10) == 10; // Returns true

var result = parseInt("010") == 10; // Returns false

请注意,parseInt在解析任何有效数据后忽略错误数据。此guid将解析为51:

var result = parseInt('51e3daf6-b521-446a-9f5b-a1bb4d8bac36', 10) == 51; // Returns true