A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-string-format-method-with-examples/ below:

Java String format() Method - GeeksforGeeks

Java String format() Method

Last Updated : 11 Jul, 2025

In Java, the String.format() method allows us to create a formatted string using a specified format string and arguments. We can concatenate the strings using this method, and at the same time, we can format the output with options such as width, alignment, decimal places, and more.

Example: In the example below, we will use the String.format() method to concatenate a string and format the output with a placeholder.

Java
// Java program to demonstrate working of format() method
class Geeks
{
    public static void main(String args[]) {
      
        String s = "GeeksforGeeks";
        
        // Concatenate string 
        // using format()
        String res = String.format("Welcome to %s!", s);

        System.out.println(res);
    }
}

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 of format() Method

There are two main versions of the format() method:

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

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

Parameters:

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.

Java
// Java program to demonstrate floating-point 
// formatting with format()
class Geeks
{
    public static void main(String args[]) {
      
        double d = 9876.54321;
        
        // Format the float number with 
        // 2 decimal places
        String s = String.format("Formatted Value: %.2f", d);

        System.out.println(s);
    }
}

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.

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 seperator.

Java
// Java program to demonstrate 
// advanced formatting using format() method
class Geeks
{
    public static void main(String args[]) {
      
        // Define a double value 
        // representing a price
        double d = 12345.6789;
        
        // Format the price with thousands 
        // separator and two decimal places
        String s = String.format("%1$,10.2f", d);

        System.out.println("Formatted Price: " + s);
    }
}

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,

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.

Java
// Java program to demonstrate 
// complex placeholder formatting
class Geeks
{
    public static void main(String args[]) {

        // Declare a double value 
        // representing the distance
        double d = 1500.75;
      
        // Declare a string value 
        // representing the unit of measurement
        String s = "kilometers";

        // Correct the argument order to match the format specifiers
        String res = String.format("%1$,7.1f %2$s", d, s);

        System.out.println(res);
    }
}

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,

Java Format Specifiers

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

Format Specifier

Data Type Output or Return value

%a

floating point                        Returns a Hex output of floating point number

%b

any type True or False

%c

character Unicode character

%d

integer  Decimal Integer

%e

floating point a decimal number in scientific notation

%f

floating point decimal number

%g

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

%h

any type Hex String of value from hashCode() method

%n

None Platform-specific line separator

%o

integer  Octal number

%s

any type String value

%t

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

%x

integer Hex string

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4