All TalkersCode Topics

Follow TalkersCode On Social Media

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

Java Round To 2 Decimal Places

Last Updated : Mar 11, 2024

Java Round To 2 Decimal Places

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);
    }
}
  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 2 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 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);
    }
}
  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 2 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 2 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 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.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪