如何在JavaScript中将字符串转换为整数?
在JavaScript中将字符串转换为整数是一项简单的任务,可以使用多种方法实现。将字符串转换为整数对于执行精确的数学运算、比较和验证用户输入非常有用。本文将讨论在JavaScript中将字符串转换为整数的各种方法。
在本文中,我们有一个字符串值,我们的任务是将字符串转换为JavaScript中的整数。
在JavaScript中将字符串转换为整数的方法
以下是本文将讨论的在JavaScript中将字符串转换为整数的方法,包括分步说明和完整的示例代码。
- 使用parseInt方法转换为整数
- 使用位运算符转换为整数
- 使用双波浪号转换为整数
- 使用Math.floor方法转换为整数
- 使用Math.round方法转换为整数
- 使用Math.ceil方法转换为整数
- 使用Math.trunc方法转换为整数
- 使用BigInt构造函数转换为整数
使用parseInt方法转换为整数
在这种方法中,我们使用了**parseInt()**方法将字符串转换为JavaScript中的整数。它以字符串作为参数,并将字符串转换为整数值。
示例
这是一个使用**parseInt()**方法将字符串转换为整数的示例。
<html>
<body>
<div id="str"></div>
<div id="num"></div>
<script>
let x=document.getElementById("str");
let str = "123.45";
x.innerHTML = "Input String: " + str
+ ", It is of type: " +typeof str;
let res=document.getElementById("num");
let num = parseInt(str);
res.innerHTML = "Result: " + num
+ ", It is of type: " +typeof num;
</script>
</body>
</html>
使用位运算符转换为整数
在这种方法中,我们使用了位运算符将字符串转换为JavaScript中的整数。我们使用了按位非运算符,它反转操作数,然后将其转换为32位有符号整数,从而将字符串转换为整数。
示例
这是一个使用按位**非**运算符将字符串转换为整数的示例。
<html>
<body>
<div id="str"></div>
<div id="num"></div>
<script>
let x=document.getElementById("str");
let str = "123.12";
x.innerHTML = "Input String: " + str
+ ", It is of type: " +typeof str;
let res=document.getElementById("num");
let num = ~str;
res.innerHTML = "Result: " + num
+ ", It is of type: " +typeof num;
</script>
</body>
</html>
**注意:**我们还可以使用其他位运算符,例如OR(|), 左移(<< 0), 右移(>> 0) 和XOR(^) 运算符将字符串转换为JavaScript中的整数。
使用双波浪号转换为整数
在这种将字符串转换为JavaScript中整数的方法中,我们使用了**双波浪号(~~)**运算符。它两次应用非运算符,将字符串转换为整数,强制进行自动转换。
示例
这是一个使用**双波浪号(~~)**运算符将字符串转换为整数的示例。
<html>
<body>
<div id="str"></div>
<div id="num"></div>
<script>
let x=document.getElementById("str");
let str = "123.23";
x.innerHTML = "Input String: " + str
+ ", It is of type: " +typeof str;
let res=document.getElementById("num");
let num = ~~str;
res.innerHTML = "Result: " + num
+ ", It is of type: " +typeof num;
</script>
</body>
</html>
使用Math.floor方法转换为整数
在这种将字符串转换为JavaScript中整数的方法中,我们使用了Math.floor方法。它通过类型强制转换将字符串转换为整数,然后将整数舍入到最接近的整数值。
示例
这是一个使用**Math.floor**方法将字符串转换为整数的示例。
<html>
<body>
<div id="str"></div>
<div id="num"></div>
<script>
let x=document.getElementById("str");
let str = "123.2";
x.innerHTML = "Input String: " + str
+ ", It is of type: " +typeof str;
let res=document.getElementById("num");
let num = Math.floor(str);
res.innerHTML = "Result: " + num
+ ", It is of type: " +typeof num;
</script>
</body>
</html>
使用Math.round方法转换为整数
在这种将字符串转换为JavaScript中整数的方法中,我们使用了Math.round方法。它通过类型强制转换将字符串转换为整数,然后将整数舍入到最接近的整数值,其中小数0.5作为临界点。
示例
这是一个使用**Math.round**方法将字符串转换为整数的示例。
<html>
<body>
<div id="str"></div>
<div id="num"></div>
<script>
let x=document.getElementById("str");
let str = "123.6";
x.innerHTML = "Input String: " + str
+ ", It is of type: " +typeof str;
let res=document.getElementById("num");
let num = Math.round(str);
res.innerHTML = "Result: " + num
+ ", It is of type: " +typeof num;
</script>
</body>
</html>
使用Math.ceil方法转换为整数
在这种将字符串转换为JavaScript中整数的方法中,我们使用了Math.ceil方法。它通过类型强制转换将字符串转换为整数,然后将整数舍入到最接近的向上整数值,即使小数位非常小。
示例
这是一个使用**Math.ceil**方法将字符串转换为整数的示例。
<html>
<body>
<div id="str"></div>
<div id="num"></div>
<script>
let x=document.getElementById("str");
let str = "123.01";
x.innerHTML = "Input String: " + str
+ ", It is of type: " +typeof str;
let res=document.getElementById("num");
let num = Math.ceil(str);
res.innerHTML = "Result: " + num
+ ", It is of type: " +typeof num;
</script>
</body>
</html>
使用Math.trunc方法转换为整数
在这种将字符串转换为JavaScript中整数的方法中,我们使用了Math.trunc方法。它通过截断小数位返回数字的整数部分。
示例
这是一个使用**Math.trunc**方法将字符串转换为整数的示例。
<html>
<body>
<div id="str"></div>
<div id="num"></div>
<script>
let x=document.getElementById("str");
let str = "123.123";
x.innerHTML = "Input String: " + str
+ ", It is of type: " +typeof str;
let res=document.getElementById("num");
let num = Math.trunc(str);
res.innerHTML = "Result: " + num
+ ", It is of type: " +typeof num;
</script>
</body>
</html>
使用BigInt构造函数转换为整数
在这种将字符串转换为JavaScript中整数的方法中,我们使用了BigInt构造函数。它将字符串转换为**BigInt**,如果输入字符串不是有效的整数(即包含小数或非数字字符),则会**抛出错误**。
示例
这是一个使用**BigInt**构造函数将字符串转换为整数的示例。
<html>
<body>
<div id="str"></div>
<div id="num"></div>
<script>
let x=document.getElementById("str");
let str = "123444444444";
x.innerHTML = "Input String: " + str
+ ", It is of type: " +typeof str;
let res=document.getElementById("num");
let num = BigInt(str);
res.innerHTML = "Result: " + num
+ ", It is of type: " +typeof num;
</script>
</body>
</html>
结论
在JavaScript中将字符串转换为整数是一项非常简单易行的任务。在本文中,我们讨论了八种方法:使用**parseInt()**方法、**位运算符**、**双波浪号**运算符、**Math.floor()**方法、**Math.round()**方法、**Math.ceil()**方法、**Math.trunc**方法以及使用**BigInt**构造函数。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP