Rounding decimal numbers can be useful while dealing with numbers in various problems. Be it a demand for some calculations or just for a soothing presentation of figures to someone, it can be really helpful. In JavaScript, we can use several methods to round to 2 decimal places. This article will discuss the methods including toFixed() and Math.round(). We will understand it better by writing code in JavaScript.
How to Round A Number to 2 Decimal Places in JS?
Rounding numbers is an operation in programming that is used to make data presentable in a more readable format so that it is better understood by us. More formally, it refers to approximating a number to a specified degree of accuracy. Let’s understand it better with an example:
Original Number: 6.892
Rounding to one decimal place: 6.9
Similarly, we can round it to any number of places we want. We will specifically explore how to round the numbers to 2 decimal places. Let’s see different methods for the same.
1) toFixed() Method
The toFixed() method is a built-in JavaScript function that allows us to round numbers to a specified number of decimal places. It can be applied to the numbers, and it returns the rounded number in the form of a string. We can convert the string back to a numeric value according to our requirements.
The toFixed() method is invoked on a number and a single parameter i.e. the number of decimal places to round to is passed in it.
Following is the syntax for using the toFixed() method:
number.toFixed(digits)
Where ‘number’ is the number to be rounded, and ‘digits’ are the desired number of decimal places.
Let’s check out an example to use it:
const number = 5.6789; const rounded = number.toFixed(2); console.log("Result of toFixed method:", rounded); console.log("Data type of output is:", typeof rounded) const convertedOutput = Number(rounded) console.log("Data type after conversion", typeof convertedOutput)
Output:
Result of toFixed method: 5.68
Data type of output is: string
Data type after conversion number
In this example, we first rounded off the number using the toFixed() method. The output is of string data type, which can be checked by using the typeof keyword. We can convert string to number by using the Number() function. Finally, the data type of converted output is displayed.
However, there is one issue that you might have noticed, which is that it returns the output in the form of a string, not a numeric value. This can lead to unexpected behavior, especially when performing mathematical operations or comparisons.
To fix this limitation, you should convert the output back to its numeric representation, as we did in the example. Various methods can be used like pareseFloat() and Number() to perform the conversion.
2) Math.round() Function
We can also round the numbers in JavaScript using the Math.round() function. Unlike the toFixed() method, which returns a string representation of the rounded number, Math.round() returns the nearest integer value. Let’s explore its syntax.
The Math.round() function takes a single parameter, the number to be rounded. It rounds the number to the nearest integer and returns the output.
Math.round(number)
If we want to round the number to 2 decimal places, we need to multiply the number by 100 and then pass it to the function, and finally divide the rounded value by 100.
Math.round(number*100)/100
Let’s look at an example to understand how it works:
const number = 5.6789; const rounded = Math.round(number * 100) / 100; console.log("Result of Math.round() method:", rounded); console.log("Data type of output is:", typeof rounded)
Output:
Result of Math.round() method: 5.68
Data type of output is: number
In this example, we first multiplied the number by 100 to shift the decimal places two positions to the right. We then use the Math.round() function to round the resulting value to the nearest integer. Finally, we divide the rounded value by 100 to shift the decimal places back to their original position.
When rounding negative numbers, we need to note that the Math.round() function rounds towards the nearest integer, with half values rounding towards positive infinity. This means that -0.5 will round to -0 rather than -1. If you need negative numbers to round towards negative infinity, you can use the Math.floor() function instead.
3) User-defined Function
You must be thinking it’s a lot of hassle to spend time deciding which one to use each time. Don’t worry we got you covered. There is an easy way to just create your custom function and use it to round the numbers.
Before moving on to an example, let’s create a function first:
function roundToTwo(number) {
return +(Math.round(number + "e+2") + "e-2");
}
In this function, we use the Math.round() function to round the number to the nearest integer.
However, as we want to round to two decimal places, we first convert the number to a string using “e+2” to shift the decimal places two positions to the right. We then add “e-2” to shift the decimal places back to their original position. Finally, we use the unary plus operator + to convert the string back to a numeric value.
Let’s use this function to round the same number:
function roundToTwo(number) { return +(Math.round(number + "e+2") + "e-2"); } const number = 5.6789; const rounded = roundToTwo(number); console.log("Result:", rounded);
Output:
Result: 5.68
In this example, we simply used that function to round the number. We just need to call the function we created and pass the number as a parameter.
Conclusion
Rounding numbers to two decimal places is a common task in JavaScript. In this article, we explored three different methods for achieving this: the toFixed() method, the Math.round() function, and a user-defined function. You can use the suitable method according to your needs. If you are still not sure, you can try our online JavaScript assignment help from top industry experts.