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 |
- Space format specifier : When creating columns of numbers, it is sometimes very useful to print a space before a positive number so that positive and negative number get aligned. To do this, space format specifier can be used.
Syntax:
Formatter().format("% d", -111); Formatter().format("% d", 111); Output: -111 111Example:
// Java program to demonstrate// the space format specifierimportjava.util.*;classGFG {publicstaticvoidmain(String args[]){// create Formatter class objectFormatter formatter =newFormatter();// Use Space format specifierformatter.format("%d", -111);System.out.println(formatter);formatter =newFormatter();formatter.format("% d",111);System.out.println(formatter);formatter =newFormatter();formatter.format("% d", -222);System.out.println(formatter);formatter =newFormatter();formatter.format("% d",222);System.out.println(formatter);}}Output:-111 111 -222 222
- + Sign Specifier: This adds the + sign before positive numeric value, and has no effect on negative numeric value.
Syntax:
Formatter().format("%+d", 111); Output: +111Example:
// Java program to demonstrate// the + sign Specifier format specifiers.importjava.util.*;classGFG {publicstaticvoidmain(String args[]){// create Formatter class objectFormatter formatter =newFormatter();// + sign specifierformatter =newFormatter();formatter.format("%+d",111);System.out.println(formatter);// + sign specifier// on - sign, it will have no effectformatter =newFormatter();formatter.format("%+d", -111);System.out.println(formatter);}}Output:+111 -111
- ( specifier: This specifier puts the negative numeric values inside the parentheses, and has no effect on the positive numeric values.
Syntax:
Formatter().format("%(d", -111); Formatter().format("%(d", 111); Output: (111) 111Example:
// Java program to demonstrate// the ( Specifiers format specifiers.importjava.util.*;classGFG {publicstaticvoidmain(String args[]){// create Formatter class objectFormatter formatter =newFormatter();// ( Specifierformatter =newFormatter();formatter.format("%(d", -111);System.out.println(formatter);formatter =newFormatter();formatter.format("%(d",111);System.out.println(formatter);}}Output:(111) 111
- Comma, Specifier: For displaying large numbers, it is often useful to add grouping separators by comma (, ). For example, the value is 1000000 more easily read when formatted as 1, 000, 000. To add grouping specifiers (, ) use the comma(, ) Specifier.
Syntax:
Formatter().format("%, d", 1000000); Output: 1, 000, 000Example:
// Java program to demonstrate// the comma format specifiers.importjava.util.*;publicclassGFG {publicstaticvoidmain(String args[]){// create Formatter class objectFormatter formatter =newFormatter();// comma Specifierformatter =newFormatter();formatter.format("%, d",1000000);System.out.println(formatter);// comma Specifierformatter =newFormatter();formatter.format("%, .3f",32659526566.4521);System.out.println(formatter);}}Output:1, 000, 000 32, 659, 526, 566.452
- Left Justification(-) Specifier: By default all output is right-shifted. That is, if the field width is longer than the data printed, data will be placed on the right side of the field. One can force output to be left-justified by placing a minus sign directly after the %. For instance, %-20.4f left justifies a floating-point number with two decimal places in a 20-character field.
Syntax:
Formatter().format("|%-20.4f|", 1234.1234); Output: | 1234.1234| |1234.1234 |Example:
// Java program to demonstrate// the left justification format specifiers.importjava.util.*;classGFG {publicstaticvoidmain(String args[]){// create Formatter class objectFormatter formatter =newFormatter();// right justify by defaultformatter =newFormatter();formatter.format("|%20.4f|",1234.1234);System.out.println(formatter);// left justifyformatter =newFormatter();formatter.format("|%-20.4f|",1234.1234);System.out.println(formatter);}}Output:| 1234.1234| |1234.1234 |
- The %n format specifiers:
The %n format specifier is different from the others in that it doesn’t take arguments. It is simply an escape sequence that inserts a character into the output. The %n inserts a newline. It can’t be entered directly into the format string.// Java program to demonstrate// the newline %n format specifiers.importjava.util.*;publicclassGFG {publicstaticvoidmain(String args[]){// create Formatter class objectFormatter formatter =newFormatter();// newline format specifierformatter.format("Geeks %nFor %nGeeks");// Print the outputSystem.out.println(formatter);}}Output:Geeks For Geeks
- The %% format specifiers:
The %% format specifier is different from the others in that it doesn’t take arguments. It is simply an escape sequence that inserts a character into the output. The %% inserts a % sign. It can’t be entered directly into the format string.// Java program to demonstrate// the %% format specifiers.importjava.util.*;publicclassGFG {publicstaticvoidmain(String args[]){// create Formatter class objectFormatter formatter =newFormatter();// %% format specifierformatter.format("10 %% 4 = 2");// Print the outputSystem.out.println(formatter);}}Output:10 % 4 = 2
- The %x %X format specifiers:
The %x or %X format specifier is is used to represent the integer Hexadecimal value. %x displays the hexadecimal values with lowercase alphabets whereas the %X specifier displays the hexadecimal values with uppercase alphabets.// Java program to demonstrate// the integer-Hexadecimal %x and %X// format specifiers.importjava.util.*;publicclassGFG {publicstaticvoidmain(String args[]){// create Formatter class objectFormatter formatter =newFormatter();// %x format specifier// It prints the number in Hexadecimal// with lowercase alphabetsformatter.format("%x",250);// Print the outputSystem.out.println("LowerCase Hexadecimal"+" using %x: "+ formatter);// %X format specifier// It prints the number in Hexadecimal// with uppercase alphabetsformatter =newFormatter();formatter.format("%X",250);// Print the outputSystem.out.println("UpperCase Hexadecimal"+" using %X: "+ formatter);}}Output:LowerCase Hexadecimal using %x: fa UpperCase Hexadecimal using %X: FA
- The %e %E format specifiers:
The %e or %E format specifier is is used to represent the Scientific Notation of a value. %e displays the Scientific Notation with lowercase alphabets whereas the %E specifier displays the Scientific Notation with uppercase alphabets.// Java program to demonstrate// the Scientific Notation %e and %E// format specifiers.importjava.util.*;publicclassGFG {publicstaticvoidmain(String args[]){// create Formatter class objectFormatter formatter =newFormatter();// %e format specifier// It prints the number in Scientific Notation// with lowercase alphabetsformatter.format("%e",123.1234);// Print the outputSystem.out.println("LowerCase Scientific Notation"+" using %e: "+ formatter);// %E format specifier// It prints the number in Scientific Notation// with uppercase alphabetsformatter =newFormatter();formatter.format("%E",123.1234);// Print the outputSystem.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
- Precision Formats
A precision specifier can be applied to the %f, %e, %g, and %s format specifiers.// Java program to demonstrate// Prrecision Format specifiersimportjava.util.Formatter;publicclassGFG {publicstaticvoidmain(String args[]){// Create the Formatter instanceFormatter formatter =newFormatter();// added floating-point data// using the %f or %e specifiers,// Format to 2 decimal places// in a 16 character field.formatter =newFormatter();formatter.format("%16.2e",123.1234567);System.out.println("Scientific notation to 2 places: "+ formatter);// Format 4 decimal places.formatter =newFormatter();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 shorterformatter =newFormatter();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 =newFormatter();formatter.format("%.15s","12345678901234567890");System.out.println("String notation to 15 places: "+ formatter);// Format into 10 digitformatter =newFormatter();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


