如何在 JavaScript 中向下取整一個數字?
本教程將教會我們如何在 JavaScript 中向下取整一個數字。向下取整數字的含義是“_返回等於或小於當前浮點數的整數_”。
例如,如果我們**向下取整**數字**9.99**,則輸出結果為**9**,同樣,對於**8.01**,我們得到 8 作為輸出。簡單來說,如果在數軸上表示當前數字,則必須返回位於當前數字左側的最近整數。
在本教程中,我們將介紹三種在 JavaScript 中向下取整數字的不同方法。
使用 Math.floor() 函式
使用 按位 運算子
使用 模運算 的自定義方法
所有方法都將逐一解釋如下。
使用 Math.floor() 函式
Math.floor() 是數學庫中 JavaScript 的內建方法。使用者需要將數字作為引數傳遞,它會返回向下取整後的數字。
語法
在 JavaScript 中使用 Math.floor() 方法的語法如下所示。
Math.floor( number );
引數
number - 它是使用者需要向下取整的數字輸入。
示例
在下面的示例中,我們將演示如何使用 Math.floor() 函式向下取整數字。
<!DOCTYPE html>
<html>
<head>
<title> Round down number </title>
</head>
<body>
<h2> The Math.floor() Method </h2>
<p> Result after rounding down the 10234.2433341 using the Math.floor() method − </p>
<p id="output"> </p>
<script>
function roundDown(number) {
let result = Math.floor(number);
output.innerHTML = result;
}
let number = 10234.2433341;
roundDown(number);
</script>
</body>
</html>在上面的輸出中,使用者可以看到向下取整後,10234.2433341 變成了 10234,它是 10234.2433341 最近的小整數。
使用按位運算子
如果對任何浮點數執行**按位**運算子,它會刪除該數字的小數部分。此外,這是一個非常流暢的過程,並且比上面使用 Math.floor() 庫函式的方法花費的時間更少。
使用者可以執行**按位或、雙非、右移**和**左移**運算來向下取整數字。但請記住,在使用按位運算子向下取整負數時,使用者需要從數字中減去 1,因為按位運算子僅刪除浮點數的小數部分。
在本部分中,我們將介紹僅使用按位或運算子向下取整數字的方法。使用者可以以相同的方式使用其他按位運算子。
語法
使用者可以按照以下語法使用按位運算子向下取整數字。
If( number > 0 ){ let output = number | 0; } else { let output = (number - 1) | 0; }
引數
number - 它是我們想要向下取整的輸入數字。
示例
在下面的示例中,我們建立了一個函式,使用按位或運算子向下取整正數和負數。
<!DOCTYPE html>
<html>
<head>
<title> Example: Round down number </title>
</head>
<body>
<h2>The Bitwise OR operator</h2>
<p> After rounding down the 102.33341 using the Bitwise OR operator, output is
<p id="PositiveOutput"></p>
<p> After rounding down the -102.33341 using the Bitwise OR operator, output is </p>
<p id="NegativeOutput"></p>
<script>
let PositiveOutput = document.getElementById("PositiveOutput");
let NegativeOutput = document.getElementById("NegativeOutput");
function roundDown(number) {
if (number > 0) {
let result = number | 0;
PositiveOutput.innerHTML = result;
} else {
let result = (number) | 0; // removing decimal part from the number
NegativeOutput.innerHTML = result - 1; //round down the number
}
}
// pass number as an function argument
roundDown(102.33341);
roundDown(-102.33341);
</script>
</body>
</html>使用者可以觀察到,我們可以透過刪除小數部分來向下取整正數,並透過刪除小數部分並從輸出中減去 1 來向下取整負數。
使用模運算的自定義方法
我們將在這種方法中使用模運算子來解決我們的問題。當我們對任何浮點數取模 1 時,它會返回該數字的小數部分。如果我們從該數字中減去數字的小數部分,則意味著我們已經將其向下取整。
此外,與上述方法一樣,此方法僅刪除數字的小數部分。因此,對於負數,使用者必須從輸出中減去 1 以向下取整數字。
使用者可以檢視以下示例。
10.2345 % 1 // returns the 0.2345 10.2345 - 10.2345 % 1 // returns 10.
語法
if( number > 0 ){ let output = number – number % 1; } else { let output = number –number %1 - 1; }
示例
<!DOCTYPE html>
<html>
<body>
<h2> Round down a number in JavaScript. </h2>
<h4> After rounding down the 99.99999 using the Modulo operator, output is </h4>
<p id="PositiveOutput"> </p>
<h4> After rounding down the -99.99999 using the Modulo operator, output is </h4>
<p id="NegativeOutput"> </p>
<script>
let PositiveOutput = document.getElementById("PositiveOutput");
let NegativeOutput = document.getElementById("NegativeOutput");
// rounding down the number using the modulo operator.
function roundDown(floatNumber) {
if (floatNumber> 0) {
let result = floatNumber - floatNumber % 1;
PositiveOutput.innerHTML = result;
} else {
let result = floatNumber - floatNumber % 1; // removing decimal part from the number
NegativeOutput.innerHTML = result - 1; //round down the
}
}
// pass number as an function argument
roundDown(99.99999);
roundDown(-99.99999);
</script>
</body>
</html>結論
以上三種方法中,向下取整數字最快的方法是第二種方法;因為按位運算比其他運算花費的時間更少。第三種方法也比第一種方法快。但我們需要關注的是,在第二種和第三種方法中,必須為正整數和負整數建立不同的情況。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP