如何在 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 庫函式相比,位運算去除數字小數部分所需的時間顯著減少。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP