Rounding A Double To 2 Decimal Places In Java
Last Updated : Mar 11, 2024
IN - Java | Written & Updated By - Pragati
In this article we will show you the solution of rounding a double to 2 decimal places in java, rounding a double to a specific decimal place is a common requirement 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 Rounding A Double To 2 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 double value to two decimal places, we can utilize this method along with some arithmetic operations.
public class DoubleRounding { 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 DoubleRounding 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 double value to two decimal places and format it accordingly.
import java.text.DecimalFormat; public class DoubleRounding { 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 DoubleRounding 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.
- 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 :-
we explored two approaches to round a double value to two 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 double value and round it to two decimal places.
Understanding how to round doubles 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 rounding a double to 2 decimal places in java helps you and the steps and method mentioned above are easy to follow and implement.