printf & String.format

 

Starting with version 5.0, Java includes a method named printf that can be used to produce output in a specific format.

 

The Java method printf is similar to the print method Like print, printf does not advance the output to the next line System.out.printf can have any number of arguments

 

The first argument is always a format string that contains one or more format specifiers for the remaining arguments

 

All the arguments except the first are values to be output to the screen.

 

double price = 19.8;

 

System.out.print("$");

 

System.out.printf("%6.2f", price);

 

System.out.println(" each");

 

will output  $ 19.80 each

 

The format string "%6.2f" indicates the following:

 

End any text to be output and start the format specifier (%)

 

Display up to 6 right-justified characters, pad fewer than six

characters on the left with blank spaces (i.e., field width is 6)

 

Display exactly 2 digits after the decimal point (.2)

 

Display a floating point number, and end the format specifier

(i.e., the conversion character is f)

 

 

import java.util.*;

 

class UserInputDemo1 

{

    public static void main(String[] args)

    {

 

        float floatVar=12.56f;

 

        int intVar=14;

 

        String stringVar="Hello";

 

        System.out.printf("The value of the float variable is "+

 

        "%f%n, while the value of the integer "+

 

        "variable is %d%n, and the string "+

 

        "is %s%n", floatVar, intVar, stringVar);

 

        String fs;

 

        fs=String.format("The value of the float variable is "+

 

        "%f%n, while the value of the integer "+

 

        "variable is %d%n, and the string "+

 

        "is %s%n", floatVar, intVar, stringVar);

 

        System.out.println(fs);

    }

}

 

OUTPUT

 

The value of the float variable is 12.560000

 

, while the value of the integer variable is 14

 

, and the string is Hello

 

The value of the float variable is 12.560000

 

, while the value of the integer variable is 14

 

, and the string is Hello

 

 

public class Main 

{

    public static void main(String[] args)

    {

 

            double myNumber = 123456.78678;

 

            String result;

 

            // Default

 

            result = String.format("%f", myNumber);

 

            System.out.println(result);

 

            // Two decimal digits

 

            result = String.format("%.2f", myNumber);

 

            System.out.println(result);

 

            // No decimal digits

 

            result = String.format("%.0f", myNumber);

 

            System.out.println(result);

 

            // No decimal digits but keep the decimal point

 

            result = String.format("%#.0f", myNumber);

 

            System.out.println(result);

 

            // Group digits

 

            result = String.format("%,.2f", myNumber);

 

            System.out.println(result);

 

            // Scientific notation with two digits of precision

 

            result = String.format("%.2e", myNumber);

 

            System.out.println(result);

    }

}


OUTPUT

123456.786780

123456.79

123457

123457.

123,456.79

1.23e+05