Open In App

Format Specifiers in Java

Format specifiers begin with a percent character (%) and terminate with a “type character, ” which indicates the type of data (int, float, etc.) that will be converted the basic manner in which the data will be represented (decimal, hexadecimal, etc.) The general syntax of a format specifier is

% [flags] [width] [.precision] [argsize] typechar

The format() method of Formatter class accepts a wide variety of format specifiers. When an uppercase specifier is used, then letters are shown in uppercase. Otherwise, the upper- and lowercase specifiers perform the same conversion. 
 



Format Specifier Conversion Applied
%% Inserts a % sign
%x %X Integer hexadecimal
%t %T Time and Date
%s %S String
%n Inserts a newline character
%o Octal integer
%f Decimal floating-point
%e %E Scientific notation
%g Causes Formatter to use either %f or %e, whichever is shorter
%h %H Hash code of the argument
%d Decimal integer
%c Character
%b %B Boolean
%a %A Floating-point hexadecimal
Formatter().format("% d", -111);
Formatter().format("% d", 111);

Output:
-111
 111

Example: 




// Java program to demonstrate
// the space format specifier
 
import java.util.*;
 
class GFG {
    public static void main(String args[])
    {
 
        // create Formatter class object
        Formatter formatter = new Formatter();
 
        // Use Space format specifier
        formatter.format("%d", -111);
        System.out.println(formatter);
 
        formatter = new Formatter();
        formatter.format("% d", 111);
        System.out.println(formatter);
 
        formatter = new Formatter();
        formatter.format("% d", -222);
        System.out.println(formatter);
 
        formatter = new Formatter();
        formatter.format("% d", 222);
        System.out.println(formatter);
    }
}

Output:

-111
 111
-222
 222
Formatter().format("%+d", 111);

Output:
+111

Example: 




// Java program to demonstrate
// the + sign Specifier format specifiers.
 
import java.util.*;
 
class GFG {
    public static void main(String args[])
    {
 
        // create Formatter class object
        Formatter formatter = new Formatter();
 
        // + sign specifier
        formatter = new Formatter();
        formatter.format("%+d", 111);
        System.out.println(formatter);
 
        // + sign specifier
        // on - sign, it will have no effect
        formatter = new Formatter();
        formatter.format("%+d", -111);
        System.out.println(formatter);
    }
}

Output:
+111
-111
Formatter().format("%(d", -111);
Formatter().format("%(d", 111);

Output:
(111)
111

Example: 




// Java program to demonstrate
// the ( Specifiers format specifiers.
 
import java.util.*;
 
class GFG {
    public static void main(String args[])
    {
 
        // create Formatter class object
        Formatter formatter = new Formatter();
 
        // ( Specifier
        formatter = new Formatter();
        formatter.format("%(d", -111);
        System.out.println(formatter);
 
        formatter = new Formatter();
        formatter.format("%(d", 111);
        System.out.println(formatter);
    }
}

Output:
(111)
111
Formatter().format("%, d", 1000000);

Output:
1, 000, 000

Example: 




// Java program to demonstrate
// the comma format specifiers.
 
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
 
        // create Formatter class object
        Formatter formatter = new Formatter();
 
        // comma Specifier
        formatter = new Formatter();
        formatter.format("%, d", 1000000);
        System.out.println(formatter);
 
        // comma Specifier
        formatter = new Formatter();
        formatter.format("%, .3f", 32659526566.4521);
        System.out.println(formatter);
    }
}

Output:
1, 000, 000
32, 659, 526, 566.452
Formatter().format("|%-20.4f|", 1234.1234);

Output:
|           1234.1234|
|1234.1234           |

Example: 




// Java program to demonstrate
// the left justification format specifiers.
 
import java.util.*;
 
class GFG {
    public static void main(String args[])
    {
 
        // create Formatter class object
        Formatter formatter = new Formatter();
 
        // right justify by default
        formatter = new Formatter();
        formatter.format("|%20.4f|", 1234.1234);
        System.out.println(formatter);
 
        // left justify
        formatter = new Formatter();
        formatter.format("|%-20.4f|", 1234.1234);
        System.out.println(formatter);
    }
}

Output:
|           1234.1234|
|1234.1234           |




// Java program to demonstrate
// the newline %n format specifiers.
 
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
        // create Formatter class object
        Formatter formatter = new Formatter();
 
        // newline format specifier
        formatter.format("Geeks %nFor %nGeeks");
 
        // Print the output
        System.out.println(formatter);
    }
}

Output:
Geeks 
For 
Geeks




// Java program to demonstrate
// the %% format specifiers.
 
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
        // create Formatter class object
        Formatter formatter = new Formatter();
 
        // %% format specifier
        formatter.format("10 %% 4 = 2");
 
        // Print the output
        System.out.println(formatter);
    }
}

Output:
10 % 4 = 2




// Java program to demonstrate
// the integer-Hexadecimal %x and %X
// format specifiers.
 
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
        // create Formatter class object
        Formatter formatter = new Formatter();
 
        // %x format specifier
        // It prints the number in Hexadecimal
        // with lowercase alphabets
        formatter.format("%x", 250);
 
        // Print the output
        System.out.println("LowerCase Hexadecimal"
                           + " using %x: "
                           + formatter);
 
        // %X format specifier
        // It prints the number in Hexadecimal
        // with uppercase alphabets
        formatter = new Formatter();
        formatter.format("%X", 250);
 
        // Print the output
        System.out.println("UpperCase Hexadecimal"
                           + " using %X: "
                           + formatter);
    }
}

Output:
LowerCase Hexadecimal using %x: fa
UpperCase Hexadecimal using %X: FA




// Java program to demonstrate
// the Scientific Notation %e and %E
// format specifiers.
 
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
        // create Formatter class object
        Formatter formatter = new Formatter();
 
        // %e format specifier
        // It prints the number in Scientific Notation
        // with lowercase alphabets
        formatter.format("%e", 123.1234);
 
        // Print the output
        System.out.println("LowerCase Scientific Notation"
                           + " using %e: "
                           + formatter);
 
        // %E format specifier
        // It prints the number in Scientific Notation
        // with uppercase alphabets
        formatter = new Formatter();
        formatter.format("%E", 123.1234);
 
        // Print the output
        System.out.println("UpperCase Scientific Notation"
                           + " using %E: "
                           + formatter);
    }
}

Output:
LowerCase Scientific Notation using %e: 1.231234e+02
UpperCase Scientific Notation using %E: 1.231234E+02




// Java program to demonstrate
// Precision Format specifiers
 
import java.util.Formatter;
 
public class GFG {
    public static void main(String args[])
    {
 
        // Create the Formatter instance
        Formatter formatter = new Formatter();
 
        // added floating-point data
        // using the %f or %e specifiers,
        // Format to 2 decimal places
        // in a 16 character field.
        formatter = new Formatter();
        formatter.format("%16.2e", 123.1234567);
        System.out.println("Scientific notation to 2 places: "
                           + formatter);
 
        // Format 4 decimal places.
        formatter = new Formatter();
        formatter.format("%.4f", 123.1234567);
        System.out.println("Decimal floating-point"
                           + " notation to 4 places: "
                           + formatter);
 
        // Format 4 places.
        // The %g format specifier causes Formatter
        // to use either %f or %e, whichever is shorter
        formatter = new Formatter();
        formatter.format("%.4g", 123.1234567);
        System.out.println("Scientific or Decimal floating-point "
                           + "notation to 4 places: "
                           + formatter);
 
        // Display at most 15 characters in a string.
        formatter = new Formatter();
        formatter.format("%.15s", "12345678901234567890");
        System.out.println("String notation to 15 places: "
                           + formatter);
 
        // Format into 10 digit
        formatter = new Formatter();
        formatter.format("%010d", 88);
        System.out.println("value in 10 digits: "
                           + formatter);
    }
}

Output:
Scientific notation to 2 places:         1.23e+02
Decimal floating-point notation to 4 places: 123.1235
Scientific or Decimal floating-point notation to 4 places: 123.1
String notation to 15 places: 123456789012345
value in 10 digits: 0000000088

Related Article : Format Specifiers in C


Article Tags :