首页 > web前端 > js教程 > 正文

如何在JavaScript中将字符串转换为整数而不使用parseInt()函数?

WBOY
发布: 2023-08-24 21:57:08
转载
953 人浏览过

如何在JavaScript中将字符串转换为整数而不使用parseInt()函数?

parseInt() 是 JavaScript 中的一个内置函数,用于解析字符串并返回一个整数。然而,有时我们想要将一个字符串转换为整数,而不使用这个函数。在本文中,我们将探讨如何做到这一点。

使用一元加号运算符

将字符串转换为整数的一种方法是使用一元加号运算符。这个运算符将其操作数转换为一个数字。例如,以下程序将字符串“123”转换为数字123 -

示例1

<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>
登录后复制

示例2

一元加运算符也可以用于将其他值转换为数字。例如,它可以将布尔值转换为数字,如下所示 −

<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>
登录后复制

Using the Number() function

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 −

Example 1

<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>
登录后复制

Example 2

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()函数

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.

Using the Math.ceil() function

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.

Example

The following code converts the string "123" into the number 123 −

<!doctype html>
<html>
<head>  
   <title>Examples</title>
</head>
<body>  
   <div id=&#39;result1&#39;></div>  
   <div id=&#39;result2&#39;></div>  
   <div id=&#39;result3&#39;></div>  
   <script>      
      let str = &#39;123&#39;;      
      document.getElementById(&#39;result1&#39;).innerHTML ="String:" +str;      
      document.getElementById(&#39;result2&#39;).innerHTML = "Before:<br> Type: "+typeof str;      
      let num = Math.ceil(str); // num = 123            
      document.getElementById(&#39;result3&#39;).innerHTML = "After:<br> Type:"+typeof num;          
   </script>
</body>
</html>
登录后复制

在上面的代码中,我们首先使用Number()函数将字符串"123"转换为一个数字。然后,我们将这个数字传递给Math.ceil()函数,该函数返回整数123。

有几种方法可以将字符串转换为整数,而不使用parseInt()函数。这些方法包括使用一元加操作符、Number()函数、Math.floor()函数和位运算符。

以上是如何在JavaScript中将字符串转换为整数而不使用parseInt()函数?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!