Heim > Web-Frontend > js-Tutorial > Hauptteil

Wie konvertiere ich eine Zeichenfolge in JavaScript in eine Ganzzahl, ohne die Funktion parseInt() zu verwenden?

WBOY
Freigeben: 2023-08-24 21:57:08
nach vorne
953 Leute haben es durchsucht

Wie konvertiere ich eine Zeichenfolge in JavaScript in eine Ganzzahl, ohne die Funktion parseInt() zu verwenden?

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>
Nach dem Login kopieren

示例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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

使用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>
Nach dem Login kopieren

在上面的代码中,我们首先使用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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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

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

Das obige ist der detaillierte Inhalt vonWie konvertiere ich eine Zeichenfolge in JavaScript in eine Ganzzahl, ohne die Funktion parseInt() zu verwenden?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:tutorialspoint.com
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!