VOOZH about

URL: https://www.geeksforgeeks.org/java/java-string-format-method-with-examples/

⇱ Java String format() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java String format() Method

Last Updated : 2 Jun, 2026

The String.format() method in Java is used to create formatted strings by combining text with values in a specified format. It helps improve the readability and presentation of output by allowing control over number formats, alignment, precision, and other formatting options.

  • Creates a formatted string without modifying the original string.
  • Supports formatting of numbers, strings, dates, and other data types.
  • Uses format specifiers such as %s, %d, %f, and %c.

Example: Java program to demonstrate working of format() method


Output
Welcome to GeeksforGeeks!

Explanation: In the above example, the format() method of the String class is used to insert the value of "s" into the formatted string. The placeholder %s is replaced with the string value.

Syntax

public static String format(Locale locale, String form, Object... args);

public static String format(String format, Object... args);

Parameters:

  • locale: The locale value to be applied (optional).
  • format: The format string that defines how the output should look.
  • args: The arguments to be formatted as per the format string.

Return Type: The method returns a formatted string.

Exceptions:

Examples of String format() Method

1. Formatting Floating-Point Numbers

Example: Using String.format() method to show concatinate the two floating values of given variable using %2f.


Output
Formatted Value: 9876.54

Explanation: In this example, a floating-point number 9876.54321 is formatted to show only two decimal places using String.format(). The placeholder %.2f ensures that the value is rounded to two decimal places and displayed.

  • %2f: The f specifier is for floating-point numbers, and .2 means the number should be rounded to two decimal places.
  • The formatted value 9876.54 is printed with the label Formatted Value, by providing a more readable output.

2. Advanced Formatting with Decimal and Thousands Separator

In this example, we will specify the formatting options like grouping digits, for example, for large numbers and controlling the number of decimal places.

Example: Using String.format() method for advance formatting with decimal and thousands separator.


Output
Formatted Price: 12,345.68

Explanation: In the above example, it formats the floating-point number with a thousands separator and two decimal places. The number 12345.6789 is formatted to 12,345.68.

In the placeholder,

  • %1$: It refers to the first argument (d), the price value.
  • ,: It groups digits with a comma as a thousands separator.
  • 10.2f: It ensures that the floating-point number takes at least 10 characters, with 2 decimal places.

3. Complex Placeholder Formatting

In this example, we will combine multiple formatting components to customize how the arguments should appear.

Example: Using String.format() method with complex placeholder formatting.


Output
1,500.8 kilometers

Explanation: In the above example, it formats a floating-point number and a string using placeholders in the String.format() method. The number 1500.75 is formatted with a comma separator and one decimal place, and the string "kilometers" is added next to it.

In the placeholder,

  • %1$: Refers to the first argument (d), the distance value.
  • ,: Groups digits with a comma as a thousands separator.
  • 7.1f: Formats the number with one decimal place and a minimum width of 7 characters.
  • %2$s: Refers to the second argument (s), the unit, and formats it as a string.

Java Format Specifiers

The String.format() method uses format specifiers to format various types of data. Below are some common specifiers:

Format Specifier

Data TypeOutput or Return value

%a

floating point                       Returns a Hex output of floating point number

%b

any typeTrue or False

%c

characterUnicode character

%d

integer Decimal Integer

%e

floating pointa decimal number in scientific notation

%f

floating pointdecimal number

%g

floating pointdecimal number, possibly in scientific notation depending on the precision and value    

%h

any typeHex String of value from hashCode() method

%n

NonePlatform-specific line separator

%o

integer Octal number

%s

any typeString value

%t

Date/Time %t is the prefix for Date/Time conversions.

%x

integerHex string
Comment
Article Tags: