Java Program to Round a Number to n Decimal Places

Java Program to read a content of a file

What is Java Basic Input and Output

In Java, input and output operations are performed through the standard input and output streams. The standard input stream is represented by the System.in object, which is typically connected to the keyboard.

  • The standard output stream is represented by the System.out object, which is typically connected to the console.

To know more about Java Program to Round a Number to n Decimal Places read the complete article.

How to Java Program to Round a Number to n Decimal Places?

There are several ways to Java Program to Round a Number to n Decimal Places

  1. Steps to program to Round a Number to n Decimal Places.
    • Import the java.math.BigDecimal class.
    • Define the number to be rounded, for example as a double.
    • Create a BigDecimal object from the number.
    • Use the setScale method of the BigDecimal object to round the number to N decimal places, specifying the number of decimal places and a rounding mode, such as ROUND_HALF_UP.
    • Convert the BigDecimal back to a double if necessary, using the doubleValue method.
      Print or use the rounded number as needed. 

Let’s look at a Java Program to Round a Number to n Decimal Places is used to perform an operation.

Example: Java Program to Round a Number using format

Run
public class Main {
  public static void main(String[] args) {
    
    double num = 9.87;

    // Round the number to 2 decimal places
    String rounded = String.format("%.2f", num);

    // Convert the rounded string to a double and round it to the nearest whole number
    double roundedNum = Math.round(Double.parseDouble(rounded));

    System.out.println("Rounded number: " + roundedNum);
  }
}

Output

Rounded number: 10.0

Example 2 : Java Program to Round a Number using DecimalFormat

Run
import java.text.DecimalFormat;

public class Main {
  public static void main(String[] args) {
    double num = 7.78;

    // Create a DecimalFormat object with 2 decimal places
    DecimalFormat df = new DecimalFormat("#.00");

    // Format the number to 2 decimal places and round it to the nearest whole number
    double roundedNum = Math.round(Double.parseDouble(df.format(num)));

    System.out.println("Rounded number: " + roundedNum);
  }
}

Output

Rounded number: 8.0

Example 3: Java Program to Round a Number using format

Run
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    double num = 9.9;

    // Round the number to 2 decimal places
    String rounded = String.format("%.2f", num);

    // Convert the rounded string to a double and round it to the nearest whole number
    double roundedNum = Math.round(Double.parseDouble(rounded));

    System.out.println("Rounded number: " + roundedNum);
  }
}

Output

Rounded number: 10.0

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription