Java Program to print the given fractional number in 2 digit decimal format

Get a fractional number from user and display the same in two digit precision

Sample Input 1:

56.3426

Sample Output 1:

56.34

Program or Solution

				
			
					
import java.util.*;
class Demo
{
public static void main(String args[])
{
 
	float num;
        Scanner sc=new Scanner(System.in);
	System.out.println("Enter The Fractional Number:");
	num=sc.nextFloat();
       
	System.out.printf("%.2f",num);
	
}
}
			
				
			

Program Explanation

The Scanner class, which is part of the java.util package, is used to get user input. To use the Scanner class, create an object of the class and call any of the methods listed in the documentation for the Scanner class. The nextFloat() method, which is used to read Fractional Numbers, are used in our example.

The Input is stored in the variable Input1 and printed in console using println() function which is the part of System.out package.

Format Specifier "%.2f" prints value as Floating Point Number with 2digit Precision(.00).