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)
ParameterStatusDescription
localeOptionalA locale used to determine some of the formatting, such as which characters are used for decimal points and grouping separators.
formatStringRequiredA string containing placeholders for the additional arguments indicating how to format them
argsOptionalAny 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

ParameterStatusDescription
arg$OptionalA 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.
flagsOptionalA sequence of any of the following characters:
-Makes the output left-justified by adding any padding spaces to the right instead of to the left.
+Causes positive numbers to always be prefixed with "+".
<space>This prefixes a space to positive numbers, primarily so that the digits can be lined up with the digits of negative numbers.
0Pads numbers with zeroes on the left.
,Groups digits (for example by thousands) and puts separators between the groups.
widthOptionalA 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.
.precisionOptionalA .(dot) followed by a whole number indicating how many decimal digits to show in the formatted data.
conversionRequiredA 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

CharacterConversionDescription
%PercentDisplays a literal "%" character in the output
nLine breakDisplays a line break in the output.
b or BBooleanDisplays the boolean value of an argument as "true" or "false". If "B" is used then it displays "TRUE" or "FALSE" instead.
 c or CUnicode characterDisplays 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 SStringDisplays the default string representation of the argument. If "S" is used then the string will be converted to uppercase where possible.
dDecimal integerRepresents a whole number as a decimal integer.
h or HUnsigned hexadecimal integerRepresents 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.
oOctal integerRepresents a whole number as an octal integer. The "#" flag will prefix the number with "0".
x or XHexadecimal integerRepresents 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 EScientific notationRepresents 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.
ffloating point numberRepresents a floating point number. The "#" flag will force a decimal point even if there are no decimal digits.
g or GGeneral numberDisplays the shortest representation between f and e or E for a floating point number.
a or AHexadecimal floating point numberDisplay a floating point number's internal representation with hexadecimal digits.
t or TTime 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:

H24-hour format of an hour (00 to 23)
l12-hour format of an hour (01 to 12)
k24-hour format of an hour (0 to 23)
l (lowercase 'L')12-hour format of an hour (1 to 12)
MMinutes with leading zeros (00 to 59)
SSeconds with leading zeros (00 to 59) (The value 60 may occur for leap seconds)
LMilliseconds with leading zeroes (000 to 999)
NNanoseconds with leading zeroes (000000000 to 999999999)
p"am", "pm", "AM" or "PM" to indicate morning or afternoon
zDifference to Greenwich time (Example: -0800)
ZTimezone abbreviations (Examples: EST, MDT)
sThe seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
QThe milliseconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
BA full textual representation of a month (January through December)
b or hA short textual representation of a month (three letters)
AA full textual representation of a day (Example: Monday)
aA short textual representation of a day (Example: Mon)
CThe first two digits of the year (For 1970, "19" would be shown)
YA four digit representation of a year
yA two digit representation of a year
jThe day of the year with leading zeroes (001 to 366)
mA numeric representation of a month (01 to 12)
dThe day of the month (01 to 31)
eThe day of the month without leading zeros (1 to 31)
RThe time in 24-hour format (Example: 21:30)
TThe time in 24-hour format with seconds (Example: 21:30:02)
rThe time in 12-hour format with seconds (Example: 09:30:02 PM) ("AM" and "PM" are always uppercase)
DDate representation as month/day/year (Example: 12/17/23)
FDate representation as year-month-day (Example: 2023-12-17)
cFull date and time (Example: Thu Mar 28 10:51:00 EDT 2024)

Examples

Here are some examples how to use the printf() method:

ExampleDescriptionOutput
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 precision1.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.333333333336
Variant 1 - The formatted value of x is: 33333.33
Variant 2 - The formatted value of x is:  33,333.33
Variant 3 - The formatted value of x is: 33333.3333
Variant 4 - The formatted value of x is: 33,333.3333
Variant 5 - The formatted value of x is: 33,333.3
Variant 6 - The formatted value of x is: 33,333
Variant 7 - The formatted value of x is: 33,333.333
33,333.33 meters4:15 pm
January 2
Fri Jan 02 22:04:35 CET 2026

Sources: w3schools.com - Java Output printf() Method - Link