如何在 JavaScript 中向下取整一个数字?
本教程将教我们如何在 JavaScript 中向下取整一个数字。向下取整数字的含义是“返回等于或小于当前浮点数的整数”。
例如,如果我们向下取整数字9.99,我们将得到9作为输出,同样,对于8.01,我们将得到 8 作为输出。简单来说,如果您在数轴上表示当前数字,则必须返回位于当前数字左侧的最近整数。
在本教程中,我们将探讨三种不同的方法来在 JavaScript 中向下取整一个数字。
使用 Math.floor() 函数
使用 位运算符
使用 取模 运算的自定义方法
所有方法都将逐一解释如下。
使用 Math.floor() 函数
Math.floor() 是 Math 库中 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