Open In App

Java Program to Round a Number to n Decimal Places

Improve
Improve
Like Article
Like
Save
Share
Report

Floating-point numbers are decimal values, which can be rounded to n number of decimal places. There are 3 different ways to Round a Number to n Decimal Places in Java as follows: 

  1. Using format Method
  2. Using DecimalFormat Class
  3. Multiply and Divide the number by 10n (n decimal places)

Example:

Input: number = 1.41421356237, round = 3 
Output:1.414
Input: number = 0.70710678118, round = 2 
Output:0.71

Method 1: Using format Method

The decimal number can be rounded by the inbuilt format() method supported by Java. 

Syntax: 

System.out.format("%.df", number);

Parameters: The first parameter accepts d digits to round off the number, the second argument accepts a number that is to be rounded.

Example:

Java




// Java Program to Round a Number to n Decimal Places
// using format() Method
 
// Importing required classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing a double number
        double number = 3.141341435;
 
        // Rounding number to 2 decimal places
        // using format method
        System.out.format("%.2f", number);
    }
}


Output

3.14

Method 2: Using DecimalFormat Class 

DecimalFormat is a child class of the NumberFormat which is used to perform formatting of decimal numbers in java. We create an object of this class and pass in as arguments the format specified in form of #, with the number of # after decimal point indicating the number of digits we wish to output. By default, the number is rounded off to the ceiling value itself. The object of the DecimalFormat class invokes a method format() which takes as argument the number to be formatted.

Example 1:

Java




// Java Program to Round a Number to n Decimal Places
// Using DecimalFormat Class
 
// Importing required classes
import java.text.DecimalFormat;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing a double number
        double number = 145.34567;
 
        // Creating an object of DecimalFormat class
        DecimalFormat df_obj = new DecimalFormat("#.###");
 
        // format() method is used to
        // print the desired number
        System.out.println(df_obj.format(number));
    }
}


Output

145.346

Note: In case we wish to round off the number to the floor, we invoke the Java in-built class RoundingMode. It has the following values for attributes as follows:

  • FLOOR – for next nearest floor value
  • CEILING – for next nearest ceiling value

Also do remember, this method can be invoked on the DecimalFormat class supported in-built method setRoundingMode(), which takes as an argument either RoundingMode.FLOOR or CEILING, and accordingly gives us the result.

Example 2:

Java




// Java Program to Round a Number to N Decimal Places
// Using DecimalFormat Class
 
// Importing required classes
import java.math.RoundingMode;
import java.text.DecimalFormat;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring and initializing double
        double number = 9.97869896;
 
        // Creating an object of DecimalFormat class
        DecimalFormat df_obj = new DecimalFormat("#.####");
 
        // Rounding number to the next lowest value
        // using setRoundingMode()
        df_obj.setRoundingMode(RoundingMode.FLOOR);
 
        // Printing the number in desired format
        // using format() method
        System.out.println(df_obj.format(number));
    }
}


Output

9.9786

Method 3: Multiply and Divide the number with 10n (n decimal places)

 In this approach, we first Multiply the number by 10n using the pow() function of the Math class. Then the number is rounded to the nearest integer. At last, we divide the number by 10n. By doing this, we get the decimal number up to n decimal places.

Syntax:

number = Math.round(number*Math.pow(10,n))/Math.pow(10,n);

Example:

Java




// Java Program to Round a Number to n Decimal Places
// Via Multiply and Divide the number with 10^n
 
// Importing required classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring and initializing double number
        double number = 12.983665;
 
        // Getting the number of decimal places required
        int n = 3;
 
        // Rounding off the number
        // using pow() and round() method of Math class
        number = Math.round(number * Math.pow(10, n))
                 / Math.pow(10, n);
 
        // Printing the rounded number
        System.out.println(number);
    }
}


Output

12.984


Last Updated : 10 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads