在 JavaScript 中将数字四舍五入到最接近的 10
JavaScript 有用于舍入数字的本机方法。 例如,要将数字四舍五入到最接近的 10,您可以使用纯 JavaScript 函数 Math.ceil()
以及额外的除法和乘法。
同样,要将数字四舍五入到最接近的 10,您可以使用纯 JavaScript 函数 Math.round()
以及额外的除法和乘法。
在这篇 JavaScript 文章中,您将学习如何使用 Math.ceil()
和 Math.round()
函数将数字向上舍入或四舍五入到最接近的 10。
在 JavaScript 中使用 Math.ceil() 将数字向上舍入到最接近的 10
要将数字向上舍入到最接近的 10,您可以调用 Math.ceil() 函数,将除以 10 的数字作为参数传递,并将结果乘以 10,例如 Math.ceil(num / 10) * 10 。
Math.ceil 函数会将数字四舍五入到下一个最大整数并返回结果。
function roundUpNearest10(num) {
return Math.ceil(num / 10) * 10;
}
console.log(roundUpNearest10(101)); // 👉️ 110
console.log(roundUpNearest10(89.9)); // 👉️ 90
console.log(roundUpNearest10(80.01)); // 👉️ 90
console.log(roundUpNearest10(-59)); // 👉️ -50
console.log(roundUpNearest10(-60)); // 👉️ -60
下面给出的屏幕截图显示了上面代码的输出。
Math.ceil 函数将为用户处理所有繁重的工作。 例如,如果传入的数字有小数部分,Math.ceil 函数会将数字向上舍入。
另一方面,如果传递一个整数,该函数将返回该数字。 以下是使用 Math.ceil 函数的一些示例。
console.log(Math.ceil(6.01)); // 👉️ 7
console.log(Math.ceil(61.00001)); // 👉️ 62
console.log(Math.ceil(60)); // 👉️ 60
console.log(Math.ceil(-33.99)); // 👉️ -33
console.log(Math.ceil(null)); // 👉️ 0
下面给出的屏幕截图显示了上面代码的输出。
当使用 null 值调用 Math.ceil 函数时,它将返回 0。下面给出了代码示例的分步过程。
console.log(31 / 10); // 👉️ 3.1
console.log(50 / 10); // 👉️ 5
console.log(Math.ceil(31 / 10)); // 👉️ 4
console.log(Math.ceil(50 / 10)); // 👉️ 5
console.log(Math.ceil(41 / 10) * 10); // 👉️ 50
console.log(Math.ceil(60 / 10) * 10); // 👉️ 60
下面给出的屏幕截图显示了上面代码的输出。
代码中有两步:
- 将数字除以 10,并将结果四舍五入到下一个最大整数。
- 将结果乘以 10,将数字四舍五入到最接近的 10。
在 JavaScript 中使用 Math.round 将数字四舍五入到最接近的 10
要将数字四舍五入到最接近的 10,您可以调用 Math.round() 函数,传递数字除以 10 并将结果乘以 10,例如 Math.round(num / 10) * 10。
Math.round 函数将获取一个数字,将其四舍五入到最接近的整数,然后返回结果。
function roundToNearest10(num) {
return Math.round(num / 10) * 10;
}
console.log(roundToNearest10(34)); // 👉️ 30
console.log(roundToNearest10(35)); // 👉️ 40
console.log(roundToNearest10(64.9)); // 👉️ 60
console.log(roundToNearest10(-36)); // 👉️ -40
console.log(roundToNearest10(-35)); // 👉️ -30
下面给出的屏幕截图显示了上面代码的输出。
Math.round 函数将为用户处理所有繁重的工作。 此函数会将数字四舍五入为最接近的整数。
另一方面,如果数字的小数部分恰好为 0.5,则该数字将四舍五入到正无穷大方向上的下一个整数。
以下是使用 Math.round 函数的一些示例。
console.log(Math.round(8.49)); // 👉️ 8
console.log(Math.round(8.5)); // 👉️ 9
console.log(Math.round(80)); // 👉️ 80
console.log(Math.round(-84.5)); // 👉️ -84
console.log(Math.round(-84.51)); // 👉️ -85
console.log(Math.round(null)); // 👉️ 0
下面给出的屏幕截图显示了上面代码的输出。
当使用 null 值调用 Math.round 函数时,该函数返回 0。 下面给出了逐步介绍的示例。
console.log(65 / 10); // 👉️ 6.5
console.log(44 / 10); // 👉️ 4.4
console.log(Math.round(65 / 10)); // 👉️ 7
console.log(Math.round(44 / 10)); // 👉️ 4
console.log(Math.round(65 / 10) * 10); // 👉️ 70
console.log(Math.round(44 / 10) * 10); // 👉️ 40
下面给出的屏幕截图显示了上面代码的输出。
这是一个两步过程:
- 将数字除以 10,并将结果四舍五入到最接近的整数。
- 将结果乘以 10,将数字四舍五入到最接近的 10。
因此,通过这篇 JavaScript 文章的帮助,用户已经学会了如何使用 Math.ceil()
和 Math.round()
函数将数字向上舍入或四舍五入到最接近的 10。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。