JavaScript Rounding: Math.round() Guide

In JavaScript, you can use the Math.round() function to round numbers.

The Math.round() function takes a number as a parameter and returns the nearest integer. If the decimal part is greater than or equal to 0.5, it will be rounded up; if the decimal part is less than 0.5, it will be rounded down.

Here is an example of using the Math.round() function for rounding numbers.

var num1 = 4.2;
var num2 = 4.8;

console.log(Math.round(num1)); // 输出 4
console.log(Math.round(num2)); // 输出 5

In the above example, Math.round(num1) would return 4 as the decimal part 0.2 is less than 0.5, and Math.round(num2) would return 5 as the decimal part 0.8 is greater than or equal to 0.5.

bannerAds