How To Round To Two Decimal Places In Java
Last Updated : Mar 11, 2024
In this article we will show you the solution of how to round to two decimal places in java, rounding a number is a common operation when working with financial calculations, measurements, or any 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 How To Round To Two Decimal Places In Java :-
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 two decimal places, we can use 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 two 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 two 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 two 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 two 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 two decimal places in Java.
The first approach used the Math. round() method along with arithmetic operations, while the second approach utilized the DecimalFormat class.
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 how to round to two decimal places in java helps you and the steps and method mentioned above are easy to follow and implement.