In this article we will show you the solution of java round to 2 decimal places, rounding numbers is a common operation when working with financial calculations, measurements, or any other scenario where precision is important.

We will explore two approaches: using the Math.round() method and using the DecimalFormat class.

## Step By Step Guide On Java Round To 2 Decimal Places :-

### Method 1 - Using the Math.round() Method

The Math.round() method is a built-in method in Java that rounds a floating-point number to the nearest whole number.

To round a number to 2 decimal places, we can utilize this method along with some arithmetic operations.

public class NumberRounding { public static double roundToTwoDecimalPlaces(double number) { return Math.round(number * 100.0) / 100.0; } public static void main(String[] args) { double num = 3.14159; double roundedNum = roundToTwoDecimalPlaces(num); System.out.println("Original Number: " + num); System.out.println("Rounded Number: " + roundedNum); } }

- We define a class called NumberRounding and declare the roundToTwoDecimalPlaces method that takes a double number as a parameter.
- Inside the roundToTwoDecimalPlaces method, we multiply the number by 100.0 to shift the decimal point two places to the right.
- We apply the Math.round() method to round the shifted number to the nearest whole number.
- Finally, we divide the rounded number by 100.0 to shift the decimal point back to the original position.
- The method returns the rounded number.
- In the main method, we create an example double number, num, with a value of 3.14159.
- We then call the roundToTwoDecimalPlaces method, passing num as an argument to round it to 2 decimal places.
- The rounded number is stored in the roundedNum variable.
- We display the original number and the rounded number.

### Method 2 - Using the DecimalFormat Class

The DecimalFormat class in Java provides a way to format decimal numbers according to a specific pattern.

We can use this class to round a number to 2 decimal places and format it accordingly.

import java.text.DecimalFormat; public class NumberRounding { public static double roundToTwoDecimalPlaces(double number) { DecimalFormat decimalFormat = new DecimalFormat("#.00"); String formattedNumber = decimalFormat.format(number); return Double.parseDouble(formattedNumber); } public static void main(String[] args) { double num = 3.14159; double roundedNum = roundToTwoDecimalPlaces(num); System.out.println("Original Number: " + num); System.out.println("Rounded Number: " + roundedNum); } }

- We import the DecimalFormat class from the java.text package.
- We define a class called NumberRounding and declare the roundToTwoDecimalPlaces method that takes a double number as a parameter.
- Inside the roundToTwoDecimalPlaces method, we create a DecimalFormat object with the pattern #.00. This pattern specifies that we want to round the number to 2 decimal places.
- We format the number using the format() method of DecimalFormat, which returns a formatted string representing the rounded number.
- Finally, we parse the formatted string back into a double using Double.parseDouble() and return the rounded number.
- In the main method, we create an example double number, num, with a value of 3.14159.
- We then call the roundToTwoDecimalPlaces method, passing num as an argument to round it to 2 decimal places.
- The rounded number is stored in the roundedNum variable.
- We display the original number and the rounded number.

## Conclusion :-

In this tutorial, we explored two approaches to round a number to 2 decimal places in Java.

We used the Math.round() method along with arithmetic operations to achieve the desired rounding behavior.

Additionally, we utilized the DecimalFormat class to format the number and round it to 2 decimal places.

Understanding how to round numbers is crucial for accurate calculations and displaying data with appropriate precision.

Remember to choose the method that best suits your requirements based on the desired output format and rounding behavior.

I hope this article on java round to 2 decimal places helps you and the steps and method mentioned above are easy to follow and implement.