In this post, we demonstrate various methods for rounding a number by the placement of 2 decimals in JavaScript.
How to Round a Number to 2 Decimal Places Using Number.toFixed() Method?
The toFixed() method is the best practice for rounding a number by the placement of 2 decimals. It takes a specific input number that represents how many numbers to place the decimals in JavaScript. The syntax for employing this method is given below.
Syntax
The syntax is described as
- num: specifies the rounded number.
- dec_dig: represents the decimal digits for placement of decimals
Example
An example is used to enlighten the toFixed() method in JavaScript.
Code
let number = 8.5424499578;
let round_number = number.toFixed(2);
console.log(round_number);
The code is explained below:
- A number variable is utilized to store the 8.542449957 number for round-off.
- Furthermore, the number.toFixed() method is called by passing 2 as an argument for placing the decimal.
- Finally, the console.log() method is employed to display a round number for placing 2 decimals in the console window.
Output
The toFixed() method is utilized to round a number to 2 decimal places, from 8.5424499578 to 8.54.
How to Round a Number to 2 Decimal Places Using Math.round() Method?
Another method known as Math.round() is adapted for rounding a number by the placement of 2 decimals in JavaScript. The method rounds the nearest integer value by multiplying and dividing the number by 100. Let’s refer to the syntax.
Syntax
This method accepts a number (as a parameter) that will be rounded off.
Example
An example is given of employing the Math.round() method in JavaScript.
Code
let number = 4.5841682;
let round_number = Math.round(number * 100) / 100;
console.log(round_number);
In this code,
- Firstly, a number variable holds a value of 4.5841682.
- After that, the built-in method of JavaScript Math.round() is utilized to round the number.
- In this method, the number is multiplied and divided by 100 to round a number by placing 2 decimal numbers.
- In the end, the console.log() method is employed to display the rounding number in the console window.
Output
The output shows that the number 4.5841682 has been rounded off to 4.58 by placing the 2 decimal numbers. It is possible through the built-in Math.round() method of JavaScript.
Bonus: How to Create a Manual Function to Round a Number?
Both the Math.round() and toFixed() methods are limited to the specific number that the user passes them. For instance, users can create a custom function for rounding a number to 2 decimals.
Code
function user_definedMet(num)
{
return +(Math.round(num + "e+2") + "e-2");
}
console.log(user_definedMet(10.89909095));
In this code, the method user_definedMet is adapted as a custom function to round a number by passing a num variable. In this method, the Math.round() method is employed for rounding the number up or down based on the exponent e. Finally, the console.log() method is utilized to display the output of a custom function by passing a 10.89909095 number.
Output
The outcome of the above code execution rounds off from 10.89909095 to 10.9.
Conclusion
The toFixed() and Math.round() methods round the number by placing the 2 decimal numbers in JavaScript. However, both these methods are limited to returning the input number after the placement of a decimal. To overcome the issue, you can create a custom function for rounding up or down the given number. This post has demonstrated various methods of JavaScript for rounding a number by the placement of 2 decimals.