All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Round To Two Decimal Places In Java

Last Updated : Mar 11, 2024

How To Round To Two Decimal Places In Java

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);
    }
}
  1. We define a class called NumberRounding and declare the roundToTwoDecimalPlaces method that takes a double number as a parameter.
  2. Inside the roundToTwoDecimalPlaces method, we multiply the number by 100.0 to shift the decimal point two places to the right.
  3. We apply the Math.round() method to round the shifted number to the nearest whole number.
  4. Finally, we divide the rounded number by 100.0 to shift the decimal point back to the original position.
  5. The method returns the rounded number.
  6. In the main method, we create an example double number, num, with a value of 3.14159.
  7. We then call the roundToTwoDecimalPlaces method, passing num as an argument to round it to two decimal places.
  8. The rounded number is stored in the roundedNum variable.
  9. 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);
    }
}
  1. We import the DecimalFormat class from the java.text package.
  2. We define a class called NumberRounding and declare the roundToTwoDecimalPlaces method that takes a double number as a parameter.
  3. 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.
  4. We format the number using the format() method of DecimalFormat, which returns a formatted string representing the rounded number.
  5. Finally, we parse the formatted string back into a double using Double.parseDouble() and return the rounded number.
  6. In the main method, we create an example double number, num, with a value of 3.14159.
  7. We then call the roundToTwoDecimalPlaces method, passing num as an argument to round it to two decimal places.
  8. The rounded number is stored in the roundedNum variable.
  9. 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.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪