JavaScript provides methods for rounding, rounding up, and rounding down numbers.
In this article, we will explain how to use these methods and use sample programs.
Rounding off the decimal point (rounding) “round”
To round off to the nearest whole number, use the Math.round() method .
Rounds the number given as an argument to the nearest integer.
Math.round(number)
let number = 3.14;
let rounded = Math.round(number);
console.log(rounded); // 3
“celi” to round up the decimal point
To round up a number, use the Math.ceil() method .
This method rounds up the number given as an argument.
Math.celi(number);
let number = 5.678;
let rounded = Math.ceil(number);
console.log(rounded); // 6
“floor” which cuts off the decimal point
To round down numbers, use the Math.floor() method .
This method rounds down the number given as an argument.
Math.floor(number);
let number = 5.678;
let rounded = Math.floor(number);
console.log(rounded); // 5
Negative numbers can also be truncated.
let number = -3.14;
let rounded = Math.floor(number);
console.log(rounded); // -4
The Math.floor() method rounds the number given as an argument to the largest integer smaller than it.
“trunc” simply truncates the decimal point part
The Math.trunc() method is a method for truncating numbers.
Just like the Math.floor() method, it truncates the decimal parts and returns only the integer part.
Math.trunc(number);
let number = 5.678;
let rounded = Math.trunc(number);
console.log(rounded); // 5
It can also be used for negative numbers.
let number = -3.14;
let rounded = Math.trunc(number);
console.log(rounded); // -3
Note that the trunc() method returns undefined for non-numeric values.
“toFixed” rounds by specifying the number of digits after the decimal point
The toFixed() method allows you to specify the number of decimal places for rounding.
number.toFixed(number of digits);
let number = 3.141592;
let rounded = number.toFixed(3);
console.log(rounded); // 3.142
“toPrecision” rounds a number to the specified number of significant digits
The toPrecision() method can round a number to a specified number of significant digits.
number.toPrecision(number of digits);
let number = 3.141592;
let rounded = number.toPrecision(5);
console.log(rounded); // 3.1416
Compared to the toFixed() method, the toPrecision() method specifies the number of significant digits and rounds instead of the number of digits after the decimal point, so it is useful when the number is small or when performing high-precision calculations.
summary
This is a summary of how to round off, round up, and round down numbers .
method | explanation |
---|---|
Math.round | You can round numbers to the nearest integer. |
Math.ceil | You can round a number to the smallest integer greater than it. |
Math.floor | You can round a number to the largest integer below it. |
Math.trunc | Numbers can be rounded down. |
toFixed | You can specify the number of digits after the decimal point for rounding. |
to Precision | You can round a number to a specified number of significant digits. |
It turns out that there are many useful methods available for rounding numbers.
In particular, toFixed() and toPrecision() can be used to round by specifying the number of digits after the decimal point and the number of significant digits, so I think they are useful when performing highly accurate calculations.
When rounding numbers, be clear about what kind of result you want before choosing the method you want to use.
Comments