All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Round In Java

Last Updated : Mar 11, 2024

How To Round In Java

In this article we will show you the solution of how to round in java, rounding numbers is a common operation in programming when we need to simplify or approximate a value to a specific decimal place.

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

Step By Step Guide On How To Round 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 a specific decimal place, we can utilize this method along with some arithmetic operations.

public class NumberRounding {
    public static double roundToDecimalPlace(double number, int decimalPlaces) {
        double factor = Math.pow(10, decimalPlaces);
        return Math.round(number * factor) / factor;
    }
    public static void main(String[] args) {
        double num = 3.14159;
        int decimalPlaces = 2;
        double roundedNum = roundToDecimalPlace(num, decimalPlaces);
        System.out.println("Original Number: " + num);
        System.out.println("Rounded Number: " + roundedNum);
    }
}
  1. We define a class called NumberRounding and declare the roundToDecimalPlace method that takes a double number and an int decimalPlaces as parameters.
  2. Inside the roundToDecimalPlace method, we calculate the factor by raising 10 to the power of decimalPlaces. This factor is used to shift the decimal places of the number we want to round.
  3. We multiply the number by the factor and apply the Math.round() method to round it to the nearest whole number.
  4. To bring the decimal places back to the desired precision, we divide the rounded number by the factor.
  5. Finally, we return the rounded number.
  6. In the main method, we create an example double number, num, with a value of 3.14159, and an int variable decimalPlaces with a value of 2.
  7. We then call the roundToDecimalPlace method, passing num and decimalPlaces as arguments to round num 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 numbers to a desired decimal place and format them accordingly.

import java.text.DecimalFormat;
public class NumberRounding {
    public static double roundToDecimalPlace(double number, int decimalPlaces) {
        DecimalFormat decimalFormat = new DecimalFormat("#." + "0".repeat(decimalPlaces));
        String formattedNumber = decimalFormat.format(number);
        return Double.parseDouble(formattedNumber);
    }
    public static void main(String[] args) {
        double num = 3.14159;
        int decimalPlaces = 2;
        double roundedNum = roundToDecimalPlace(num, decimalPlaces);
        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 roundToDecimalPlace method that takes a double number and an int decimalPlaces as parameters.
  3. Inside the roundToDecimalPlace method, we create a DecimalFormat object by specifying the desired pattern. The pattern consists of a hash symbol (#) followed by a dot (.) and a number of zero (0) repetitions based on the decimalPlaces.
  4. We format the number using the format() method of DecimalFormat, which returns a formatted string.
  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, and an int variable decimalPlaces with a value of 2.
  7. We then call the roundToDecimalPlace method, passing num and decimalPlaces as arguments to round num 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 numbers in Java.

We used the Math.round() method to round a number to the nearest whole number or a specific decimal place by utilizing arithmetic operations.

We also utilized the DecimalFormat class to format decimal numbers and round them to a desired decimal place.

Understanding how to round numbers is essential for various applications, such as financial calculations or 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 in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪