We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
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:
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:
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:
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.
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.
The %x %X format specifiers: The %x or %X format specifier 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.
LowerCase Hexadecimal using %x: fa
UpperCase Hexadecimal using %X: FA
The %e %E format specifiers: The %e or %E format specifier 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.
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