Java String.format() method Usage Java String.format() method Usage

Page content

This article demonstrate use of String.format() in Java with many examples…

The String.format() method is used to produce formatted string, numeric, and date/time data, and locale specific output. It requires a format string and argument list as input.

1. Syntax

//Returns a formatted string using the specified format string and arguments.
static String format(String format, Object... args)	

//Returns a formatted string using the specified locale, format string, and arguments.
static String format(Locale l, String format, Object... args)	

Let’s look into format specifier before we look into examples

Syntax of format specifier
%[argument_index$][flags][width][.precision]conversion

Where,

  • optional argument_index is position of argument. The first arg is referenced by 1$, the second by 2$, etc.
  • optional flags is set of characters to modify the output format
    If not specified - The result will include a leading space for positive values
    - The result will be left-justified.
    # The result should use a conversion-dependent alternate form
    + The result will always include a sign
    0 The result will be zero-padded
    , The result will include locale-specific grouping separators
    ( The result will enclose negative numbers in parentheses
  • optional width is minimum number of characters in string output
  • optional precision is used for floating numbers to indicate numbers after decimal places.
  • required conversion is character indicating how it should be formatted
    s or S for string
    f for floating point numbers
    d for integers
    t for date and time
    n for line break

2. String Format Examples

2.1 String Template

String[] fruits = new String[] {"Mango", "Grapes", "Banana", "Kiwi"};

// String Concat
System.out.println(String.format("My favourite fruits are %1$s, %2$s, %3$s and %4$s", fruits));

//Reverse String Concat
System.out.println(String.format("My favourite fruits are %4$s, %3$s, %2$s and %1$s", fruits));

We have used formatter %1$s, %2$s, %3$s, %4$s to specify the position of string arguments and %n for line break.

Output
My favourite fruits are Mango, Grapes, Banana and Kiwi My favourite fruits are Kiwi, Banana, Grapes and Mango

2.2 String Line Break

String format can be used to create multi line string in Java

System.out.println(String.format("Address:-%nUnit 505,%n32 Cross Street", null));
Output
Address:- Unit 505, 32 Cross Street

2.3 String Padding

Following String format will add left padding of 10 characters to foo
%13s says that output will have min width of 13 including foo length, which is 3 hence (13 - 3 =) 10 left padding space.

System.out.println(String.format("%13s", "foo"));
Output
foo

3. Number Format Examples

3.1 Float Decimal Places

System.out.println(Math.PI);

// Rounding off Decimal Places      
System.out.println(String.format("%.1f", Math.PI));
System.out.println(String.format("%.5f", Math.PI));
System.out.println(String.format("%.10f", Math.PI));

We have used formatter %.1f, %.5f, and %.10f to show 1, 5 and 10 decimal places of PI respectively.

Output
3.141592653589793 3.1 3.14159 3.1415926536

3.2 Float Padding

// Min Width with leading spaces
System.out.println(String.format("%10f", Math.PI));
System.out.println(String.format("%20f", Math.PI));
System.out.println(String.format("%30f", Math.PI));

We have used formatter %10f, %20f, and %30f to show minimum 10, 20 and, 30 characters with leading space respectively. We see that string length 2.141593 is 8 characters which result in padding of 2, 12 and, 22 characters respectively.

Output
3.141593 3.141593 3.141593

3.3 Negative Floating Point Numbers

// +ve Floating Numbers
System.out.println(String.format("%+20.5f", Math.PI));     // + Flag show sign
System.out.println(String.format("%020.5f", Math.PI));     // 0 Flag for leading zeros
System.out.println(String.format("%(20.5f", Math.PI));    // ( Flag to show -ve numbers in parentheses

// -ve Floating Numbers
System.out.println(String.format("%+20.5f", -Math.PI));
System.out.println(String.format("%020.5f", -Math.PI));
System.out.println(String.format("%(20.5f", -Math.PI)); 
Output
+3.14159 00000000000003.14159 3.14159 -3.14159 -0000000000003.14159 (3.14159)

3.4 Float with locale

//Locale Specific
System.out.println(String.format(Locale.FRANCE, "%f", Math.PI));
System.out.println(String.format(Locale.FRANCE, "%f", -Math.PI));  
Output
3,141593 -3,141593

3.5 Format Number with Commas

System.out.println(String.format("%,d", 1234567890));
Output
1,234,567,890

4. Date Format Examples

4.1 LocalDateTime Format

LocalDateTime date = LocalDateTime.parse("1986-08-22T05:45:59");

System.out.println(String.format("My Birth Date is %1$te-%1$tm-%1$tY %1tT", date));
System.out.println(String.format("I was born on %1$teth %1tB, %1$tY", date));
Output
My Birth Date is 22-08-1986 05:45:59 I was born on 22th August, 1986

4.2 LocalDate Format

Format LocalDate to yyyymmdd

LocalDate date = LocalDate.parse("2020-12-25");
// yyyymmdd
System.out.println(String.format("%1$tY%1$tm%1$te", date));
Output
20201225

5. Print Format Examples

5.1 System.out.printf and System.out.format

If you have to just print formatted output and doesn’t require returned output as string, you can use System.out.printf or System.out.format instead.

String print = "Hello World !!!";

System.out.println(String.format("Saying - %s", print));

System.out.printf("Saying - %s", print);

System.out.format("Saying - %s", print);

All the three System.out print the same output

Output
Saying - Hello World !!! Saying - Hello World !!! Saying - Hello World !!!