如何在 JavaScript 中去除数字的小数部分?


在本教程中,我们将学习如何去除 JavaScript 数字中的小数部分。在使用 JavaScript 的过程中,开发人员经常需要处理数据库中存储的数字,并将浮点数和双精度数转换为纯十进制数或整数。

在这种情况下,开发人员有多种方法可以截断数字的小数部分。我们将学习几种不同的方法来解决上述问题。

  • 使用Math.trunc() 方法

  • 使用位运算符

  • 使用Math.ceil()Math.floor() 方法

使用Math.trunc() 方法

Math.trunc() 是去除 JavaScript 数字小数部分最常用的方法。它接受正数、负数或浮点数作为输入,并去除小数部分后返回整数部分。例如,当输入为5.45时,它只返回5;当输入为-5.45时,它只返回-5

语法

用户可以按照以下语法使用Math.trunc() 方法。

let decimal = Math.trunc( number )

参数

  • 数字 − 此参数表示需要去除小数部分的数字输入。

示例 1

以下示例演示了在 JavaScript 中使用Math.trunc() 方法。

<!DOCTYPE html>
<html>
<body>
   <h2>Removing the decimal part from the number.</h2>
   <p> Result fter removing the decimal part from 4.96 using Math.trunc() method.</p>
   <p id = "result"></p>
   <script type="text/javascript" >
      let number = 4.96;
      let decimal = Math.trunc( number );
      document.getElementById("result").innerHTML = decimal;
   </script>
</body>
</html>

在上面的输出中,用户可以看到我们去除了 4.96 的小数部分,它变成了 4。

使用位运算符

在这种方法中,我们将使用不同的位运算符来去除数字的小数部分。为了解决我们的问题,我们可以使用四种不同的位运算符,例如位或、双重非、右移左移运算符。我们将对输入数字执行与 0 的位或运算。同样,我们可以对输入数字执行与 0 的右移和左移运算,这将去除其小数部分。

内置的 Math 库函数比位运算需要更多的计算时间。因此,这是截断数字的最快方法。

语法

let result = ~~number // Double Not operator
let result = number | 0 // Bitwise OR operation of number with 0.
let result = number >> 0 // Right shift of number by 0.
let result = number << 0 // Left shift of number by 0.

示例 2

用户可以通过以下示例学习如何使用位运算来去除数字的小数部分。我们在下面的示例中使用了所有不同的位运算符并打印输出。

<!DOCTYPE html>
<html>
<body>
   <h2> Removing the decimal part from the number.
   <h4> After removing the decimal part from <i> -5.69 </i> using Double Not operator.</h4>
   <p id = "DoubleNot"> </p>
   <h4> After removing the decimal part from <i> 2.34 </i> using BitWise ORoperator.</h4>
   <p id = "BitwiseOR"> </p>
   <h4> After removing the decimal part from <i> 100.87 </i> using RightShift operator. </h4>
   <p id = "RightShift"> </p>
   <h4> After removing the decimal part from <i> -0.7 </i> using left Shiftoperator. </h4>
   <p id = "LeftShift"> </p>
   <script type="text/javascript" >
      // remove decimal part using the double not operator.
      let DoubleNot = document.getElementById("DoubleNot");
      let number = -5.69;
      DoubleNot.innerHTML = ~~number;

      // remove decimal part using the Bitwise OR operator.
      let BitwiseOR = document.getElementById("BitwiseOR");
      number = 2.34;
      BitwiseOR.innerHTML = number | 0;

      // remove decimal part using the Right shift operator.
      let = document.getElementById("RightShift");
      number = 100.87;
      RightShift.innerHTML = number >> 0;
   
      // remove decimal part using the left shift operator.
      let LeftShift = document.getElementById("LeftShift");
      number = -0.7;
      LeftShift.innerHTML = number << 0;
   </script>
</body>
</html>

在上面的输出中,用户可以观察到我们如何使用位运算符截断数字的小数部分,它返回小数点左边的数字。

使用 Math.ceil() 和 Math.floor() 方法

我们将在这种方法中使用Math.ceil()Math.floor() 方法。Math.ceil() 方法返回浮点数的下一个更大的整数元素。例如,对于 5.1,它返回 5。

Math.floor() 方法返回浮点数输入的前一个较小的整数。例如,对于输入 5.9,它返回 5。

语法

Math.ceil ( number )
Math.floor ( number )

示例 2

在下面的示例中,我们使用了Math.ceil() 方法和Math.floor() 方法。我们还打印了两种方法的输出。

<!DOCTYPE html>
<html>
<body>
   <h2>Removing the decimal part from the number</h2>
   <h4> After removing the decimal part from <i> 0.01 </i> using the Math.ceil() method. </h4>
   <p id = "CeilMethod"> </p>
   <h4> After removing the decimal part from <i> 6.99 </i> using the Math.floor() method. </h4>
   <p id = "FloorMethod"> </p>
   <script type="text/javascript">
      // remove decimal part using the Math.ceil() method.
      let CeilMethod = document.getElementById("CeilMethod");
      let number = 0.01;
      CeilMethod.innerHTML = Math.ceil(number);

      // remove decimal part using the Math.floor() method.
      let FloorMethod = document.getElementById("FloorMethod");
      number = 6.99;
      FloorMethod.innerHTML = Math.floor(number);
   </script>
</body>
</html>

在上面的输出中,用户可以看到Math.ceil() 函数返回了 0.01 的下一个更大元素 1,而Math.floor() 函数返回了 6.99 的前一个较小整数 6。

结论

在本教程中,我们使用了三种方法来去除数字的小数部分。最常用的方法是第一种方法,最有效的方法是第二种方法。与 JavaScript 的 Math 库函数相比,位运算去除数字小数部分所需的时间显著减少。

更新于:2023年9月6日

45K+ 次浏览

启动您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.