
parseInt() 是 JavaScript 中的一个内置函数,用于解析字符串并返回一个整数。然而,有时我们想要将一个字符串转换为整数,而不使用这个函数。在本文中,我们将探讨如何做到这一点。
将字符串转换为整数的一种方法是使用一元加号运算符。这个运算符将其操作数转换为一个数字。例如,以下程序将字符串“123”转换为数字123 -
<html>
<head>
<title>Examples</title>
</head>
<body>
<div id="result1"></div>
<div id="result2"></div>
<div id="result3"></div>
<script>
let str = "123";
document.getElementById("result1").innerHTML = typeof str;
let num = +str; // num = 123
document.getElementById("result2").innerHTML = num;
document.getElementById("result3").innerHTML = typeof num;
</script>
</body>
</html>一元加运算符也可以用于将其他值转换为数字。例如,它可以将布尔值转换为数字,如下所示 −
<html>
<head>
<title>Examples</title>
</head>
<body>
<div id="result"></div>
<script>
let bool = true;
let num = +bool; // num = 1
document.getElementById("result").innerHTML = num
</script>
</body>
</html>Another way to convert a string into a number is by using the Number() function. This function takes in an argument and returns a number. For instance, the following code converts the string "123" into the number 123 −
立即学习“Java免费学习笔记(深入)”;
<html>
<head>
<title>Examples</title>
</head>
<body>
<div id="result1"></div>
<div id="result2"></div>
<div id="result3"></div>
<script>
let str = "123";
document.getElementById("result1").innerHTML = typeof str;
let num = Number(str); // num = 123
document.getElementById("result2").innerHTML = num;
document.getElementById("result3").innerHTML = typeof num;
</script>
</body>
</html>Number()函数还可以用于将其他值转换为数字。例如,它可以将布尔值转换为数字,如下所示 -
<html>
<head>
<title>Examples</title>
</head>
<body>
<div id="result"></div>
<script>
let bool = false;
let num = Number(bool); // num = 1
document.getElementById("result").innerHTML = num
</script>
</body>
</html>Math.floor()函数返回小于或等于给定数字的最大整数。该函数可用于将字符串转换为整数。
以下代码将字符串"123"转换为数字123−
<html>
<head>
<title>Examples</title>
</head>
<body>
<div id="result1"></div>
<div id="result2"></div>
<div id="result3"></div>
<script>
let str = "123";
document.getElementById("result1").innerHTML = typeof str;
let num = Math.floor(str); // num = 123
document.getElementById("result2").innerHTML = num;
document.getElementById("result3").innerHTML = typeof num;
</script>
</body>
</html>在上面的代码中,我们首先使用Number()函数将字符串"123"转换为一个数字。然后我们将这个数字传递给Math.floor()函数,它返回整数123。
位运算符是对位值进行操作的运算符。这些运算符可以用来将字符串转换为整数。
以下代码将字符串"765"转换为数字765 −
<html>
<head>
<title>Examples</title>
</head>
<body>
<div id="result1"></div>
<div id="result2"></div>
<div id="result3"></div>
<script>
let str = "765";
document.getElementById("result1").innerHTML = typeof str;
let num = str|0;
document.getElementById("result2").innerHTML = num;
document.getElementById("result3").innerHTML = typeof num;
</script>
</body>
</html>In the above code, we use the bitwise OR operator (|) to convert the string "765" into a number. The bitwise OR operator converts its operands into 32-bit integers and then performs a bitwise OR operation on them. In this case, it converts the string "765" into the number 765.
The Math.ceil() function returns the smallest integer greater than or equal to a given number. This function can be used to convert a string into an integer.
The following code converts the string "123" into the number 123 −
<!doctype html>
<html>
<head>
<title>Examples</title>
</head>
<body>
<div id='result1'></div>
<div id='result2'></div>
<div id='result3'></div>
<script>
let str = '123';
document.getElementById('result1').innerHTML ="String:" +str;
document.getElementById('result2').innerHTML = "Before:<br> Type: "+typeof str;
let num = Math.ceil(str); // num = 123
document.getElementById('result3').innerHTML = "After:<br> Type:"+typeof num;
</script>
</body>
</html>在上面的代码中,我们首先使用Number()函数将字符串"123"转换为一个数字。然后,我们将这个数字传递给Math.ceil()函数,该函数返回整数123。
有几种方法可以将字符串转换为整数,而不使用parseInt()函数。这些方法包括使用一元加操作符、Number()函数、Math.floor()函数和位运算符。
以上就是如何在JavaScript中将字符串转换为整数而不使用parseInt()函数?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号