Java Program To Round The Number To N Decimal Places

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Program To Round The Number To N Decimal Places

In this article we will see how we can round a number to N decimal places.

Concept:

We can round decimal number  to a certain accuracy or n number of decimal places. It is used when we want to  make calculation easier & makes results easier to understand, it is applicable only when exact values are not too important.

For example:

A number is 231.434343545 and we didn’t want the exact value , in that case we can reduce the floating part to 2 or 3 decimal places, which will make the number simpler and further simple use.

After rounding that number to 2 decimal places,  the number will be à 231.43
After rounding that number to 3 decimal places,  the number will be à 231.434

Now we will see one by one approach

Method 1 : Java program to round the number to n decimal places by using formatting approach

We can round the decimal number to n decimal places by using formatting approach.

Approach:

  • Enter a decimal number.
  • Print the decimal number by formatting approach (using %df).

Program:

import java.util.Scanner;
public class Main 
{  
    public static void main(String[] args)
    {  
         // CREATING OBJECT 
        Scanner sc = new Scanner(System.in);  
        // TAKING STRING FORM USER 
        System.out.println("Enter number: ");  
        double n= sc.nextDouble();   
        // rounding number to 2 decimal places
        System.out.println("number: " + n);
        System.out.format("\n after rounding : %.3f", n);
    }
}
Output:

Enter number: 231.32525325
number: 231.32525325
after rounding : 231.325

Method 2 : Java program to round the number to n decimal places DecimalFormat class

We can round the decimal number to n decimal places by using DecimalFormat class approach . This class is a child class of NumberFormat . In this we create an object of this class and pass  the arguments as the format specified in form of ”#”, with the number of # after the decimal point , which indicate how many n places we want to store after the decimal point .

Approach:

  • Enter a decimal number .
  • Create a object of DecimalFormat by giving the ‘N‘ no of ‘#‘ after the “.” (Ex- #.####)
  • Print the decimal number by the method  format ().

Program:

import java.util.Scanner;
import java.text.DecimalFormat;
public class Main 
{  
    public static void main(String[] args)
    {  
         // CREATING OBJECT 
        Scanner sc = new Scanner(System.in);  
        // TAKING STRING FORM USER 
        System.out.print("Enter number: ");  
        double n= sc.nextDouble();   
        //creating obj. of the DecimalFormat 
         DecimalFormat d = new DecimalFormat("#.####");
        System.out.print("number: " + n);
        System.out.print("\nafter rounding : " + d.format(n));
    }
}
Output:

Enter number: 231.32525325
number: 231.32525325
after rounding : 231.3252

Method 3 : Java program to round the number to n decimal places using BigDecimal class

We can round the decimal number to n decimal places by using BigDecimal class approach .

Approach:

  • Enter a decimal number .
  • Create a object of BigDecimal class.
  • Round up the number by using setcsale() method .
  • Print that value .

Program:

import java.math.RoundingMode;
import java.util.Scanner;
import java.math.BigDecimal; 
public class Main 
{  
    public static void main(String[] args)
    {  
       // CREATING OBJECT 
        Scanner sc = new Scanner(System.in);  
        // TAKING STRING FORM USER 
        System.out.print("Enter number: ");  
        double n= sc.nextDouble();   
        System.out.print("Enter how many places you want : ");  
        int a= sc.nextInt(); 
        //creating obj. of the bigdecimal 
        BigDecimal bd = new BigDecimal(Double.toString(n));
        // rounding the value to desire n places 
        bd = bd.setScale(a, RoundingMode.HALF_UP);
        System.out.print("number: " + n);
        System.out.print("\nafter rounding : " + bd);
    }     
}

Output:

Enter number: 123.123456
Enter how many places you want : 4
number: 123.123456
after rounding : 123.1235

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Core Java Programs: