Java - System.out.printf

Table of Contents
Definition
The System.out.printf() method allows to output a formatted string. The method uses placeholders (%) in the string that are replaced by arguments.
Java syntax
System.out.printf(locale, formatString, args)| Parameter | Status | Description |
| locale | Optional | A locale used to determine some of the formatting, such as which characters are used for decimal points and grouping separators. |
| formatString | Required | A string containing placeholders for the additional arguments indicating how to format them |
| args | Optional | Any number of additional arguments to the method, their values can be formatted and displayed in the formatString. |
Placeholders
The placeholders have the form:
%[arg$][flags][width][.precision]conversion
The components in [square brackets] are optional.
Explanation
| Parameter | Status | Description | ||||||||||
| arg$ | Optional | A number followed by a $ sign which indicates which of the additional arguments to use, argument numbers start at 1. This can be replaced with a < which specifies that the argument from the previous placeholder should be used. | ||||||||||
| flags | Optional | A sequence of any of the following characters:
| ||||||||||
| width | Optional | A whole number specifying the minimum number of characters that the output should occupy. If necessary, spaces are added to the left to reach this number, or to the right if the - flag is used. | ||||||||||
| .precision | Optional | A .(dot) followed by a whole number indicating how many decimal digits to show in the formatted data. | ||||||||||
| conversion | Required | A character which indicates how an argument's data should be represented. If the character is uppercase the data will be formatted in uppercase where possible. The list of possible characters is shown in the table below. |
List of conversions
| Character | Conversion | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| % | Percent | Displays a literal "%" character in the output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| n | Line break | Displays a line break in the output. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| b or B | Boolean | Displays the boolean value of an argument as "true" or "false". If "B" is used then it displays "TRUE" or "FALSE" instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c or C | Unicode character | Displays a unicode character representation of the argument. For whole numbers, this is the unicode character that corresponds to the number. If "C" is used then the character will be converted to uppercase where possible. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| s or S | String | Displays the default string representation of the argument. If "S" is used then the string will be converted to uppercase where possible. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| d | Decimal integer | Represents a whole number as a decimal integer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| h or H | Unsigned hexadecimal integer | Represents an argument's binary data as an unsigned hexadecimal integer. If "H" is used then digits A to F are shown in uppercase. Note: For any data other than positive integers this does not represent its real value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| o | Octal integer | Represents a whole number as an octal integer. The "#" flag will prefix the number with "0". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x or X | Hexadecimal integer | Represents a whole number as a hexadecimal integer. The "#" flag will prefix the number with "0x". If "X" is used then digits A to F and the letter X are shown in uppercase | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| e or E | Scientific notation | Represents a floating point number in scientific notation. If "E" is used then the letter "E" of the representation will be uppercase. The "#" flag will force a decimal point even if there are no decimal digits. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f | floating point number | Represents a floating point number. The "#" flag will force a decimal point even if there are no decimal digits. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| g or G | General number | Displays the shortest representation between f and e or E for a floating point number. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| a or A | Hexadecimal floating point number | Display a floating point number's internal representation with hexadecimal digits. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t or T | Time or date | Displays a formatted date or time. The t or T must be followed by one more character indicating how the date or time should be formatted. If "T" is used then text parts of a date or time such as "JANUARY" will be uppercase.The following characters can be used for date and time formatting:
|
Examples
Here are some examples how to use the printf() method:
| Example | Description | Output |
| System.out.printf("%f%n", 123456.78); | %f => a floating point number %n => a new line | 123456.780000 |
| System.out.printf("%.2f%n", 123456.78); | %.2f => Two decimal digits %n => a new line | 123456.78 |
| System.out.printf("%.0f%n", 123456.78); | %.2f => no decimal digits %n => a new line | 123457 |
| System.out.printf("%#.0f%n", 123456.78); | %#.0f => no decimal digits but keep the decimal point %n => a new line | 123457. |
| System.out.printf("%,.2f%n", 123456.78); | %,.2f => group digits with coma and two decimal digits %n => a new line | 123,456.78 |
| System.out.printf("%.2e", 123456.78); | %.2e => scientific notation with two digits of precision | 1.23e+05 |
| System.out.printf("%2$,3.2f %1$s", "meters", 1260.5052); | %2$,3.2f => 2nd placeholder (%2$) grouped (,) with at least 3 character long (3) with 2 digits after the decimal point (.2) and represented as a floating point number (f). %1$s => 1st placeholder (%1$) as a string (s). | 1,260.51 meters |
| System.out.printf("%3$c %2$c %1$c", 'a', 'b', 'c'); | 3 placeholders. %3$c => c %2$c => b %1$c => a | c b a |
System.out.printf("%tl:%<tM %<tp%n", 1711638903488L); | Time %tl => 12-hours format for hour %<tM => (%<) which specifies that the argument from the previous placeholder should be used. (tM) => minutes %<tp => (%<) argument from the previous placeholder with (tp) 'am' or 'pm' format. | 3:15 pm |
| System.out.printf("%tB %<te%n", date); | Month and day. %tB => Month %<te => argument from the previous placeholder with day format. %n => a new line | March 28 |
| System.out.printf("%tc%n", date); | %tc => full date representation %n => a new line | Thu Mar 28 15:15:03 UTC 2024 |
Code example
package com.learningkoala;
import java.util.Date;
public class MyFormattedOutput {
public static void main(String[] args) {
double x = 100000.0 / 3.0;
// Unix timestamp (number of milliseconds since January 1, 1970)
long exampleTime = 1711638903488L;
Date currentDate = new Date();
System.out.println("The default value of x is: " + x);
// %.2f%n means format as floating point with 2 decimal places, %n is a new line
System.out.printf("Variant 1 - The formatted value of x is: %.2f%n", x);
// %, .2f%n - include grouping separator and format as floating point with 2
// decimal places, %n is a new line
System.out.printf("Variant 2 - The formatted value of x is: %, .2f%n", x);
// %.4f%n - format as floating point with 4 decimal places, %n is a new line
System.out.printf("Variant 3 - The formatted value of x is: %.4f%n", x);
// %,.4f%n - include grouping separator and format as floating point with 4
// decimal places, %n is a new line
System.out.printf("Variant 4 - The formatted value of x is: %,.4f%n", x);
// %,.1f%n - include grouping separator and format as floating point with 1
// decimal place, %n is a new line
System.out.printf("Variant 5 - The formatted value of x is: %,.1f%n", x);
// %,.0f%n - include grouping separator and format as floating point with no
// decimal places, %n is a new line
System.out.printf("Variant 6 - The formatted value of x is: %,.0f%n", x);
// %,.3f%n - include grouping separator and format as floating point with 3
// decimal places, %n is a new line
System.out.printf("Variant 7 - The formatted value of x is: %,.3f%n", x);
// %2$,3.2f %1$s - positional arguments, first is a string, second is a float
// with grouping separator and 2 decimal places
System.out.printf("%2$,3.2f %1$s", "meters", x);
// %tl:%tM %tp%n - format time in hours:minutes am/pm
System.out.printf("%tl:%<tM %<tp%n", exampleTime);
// %tB %<te%n - format date as full month name and day of month
System.out.printf("%tB %<te%n", currentDate);
// %tc%n - format date and time in default format (full date representation)
System.out.printf("%tc%n", currentDate);
}
}Ouput:
The default value of x is: 33333.333333333336Variant 1 - The formatted value of x is: 33333.33Variant 2 - The formatted value of x is: 33,333.33Variant 3 - The formatted value of x is: 33333.3333Variant 4 - The formatted value of x is: 33,333.3333Variant 5 - The formatted value of x is: 33,333.3Variant 6 - The formatted value of x is: 33,333Variant 7 - The formatted value of x is: 33,333.33333,333.33 meters4:15 pmJanuary 2Fri Jan 02 22:04:35 CET 2026
Sources: w3schools.com - Java Output printf() Method - Link
