Java Program To Format The Number

Write Java program to format the number

// WAP for formatting the number

import java.util.Scanner;
public class NumberFormat
{
    public static void main(String []args) 
    {
       float n;
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the floating point number :--> ");
       n = sc.nextFloat();

       System.out.println("The floating point number is :"+n);

       //display 2 digit after decimal
       System.out.println("The floating point number 0.2f is:"+String.format("%.2f", n));

        //display 4 digit after decimal in width of 10 & right align
        System.out.println("The floating point number 10.4f is:"+String.format("%10.4f", n));

    }
}

Output:


<< Previous | Next >>


Leave a Reply

Your email address will not be published.