parseInt() 函式在 JavaScript 中的使用


parseInt() 函式接受兩個引數,一個是表示數字的字串,另一個是表示基數的數字,並返回指定基數的整數。

語法

其語法如下所示

num.parseInt('4524', 8);

示例

 即時演示

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var result = parseInt('4524', 8);
      document.write("Result: " + result);
   </script>
</body>
</html>

輸出

Result: 2388

示例

 即時演示

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var result1 = parseInt('4524', 8);
      document.write("Result: " + result1);
      document.write("<br>");
      var result2 = parseInt('4524', 10);
      document.write("Result: " + result2);
      document.write("<br>");
      var result3 = parseInt('4524', 16);
      document.write("Result: " + result3);
   </script>
</body>
</html>

輸出

Result: 2388
Result: 4524
Result: 17700

更新於: 2020 年 6 月 25 日

456 次瀏覽

開啟你的 職業生涯

完成培訓,獲取認證

開始學習
廣告
© . All rights reserved.