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

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

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

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

立即学习Java免费学习笔记(深入)”;

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='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在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号