×

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments.

Syntax:

public static String format(String format , Object… args)

Parameter:

locale : It specifies locale value to be applied on the format() method.

format: It specifies the format of the output String

args : It specifies the number of arguments for the format String.It may be zero or more

Returns:

It returns a formatted String

Java String format() Example 1

public class JavaStringFormatEx1 {
    public static void main(String args[])
    {
        String s1 = "Tutorial And Example";
        // Concatenation of two strings
        String str1 = String.format("Welcome to %s", s1);
        // Output is given upto 11 decimal places
        String str2 = String.format("value is %.11f", 75.23456);
        System.out.println(str1);
        System.out.println(str2);
    }
}

Output:

Welcome to Tutorial And Example
value is 75.23456000000
value is 47.65734000

Java String format() Example 2

public class JavaStringFormatEx2
{ 
public static void main(String args[])
{ 
String s1="Nitish"; 
String f1=String.format("name is %s",s1); 
String f2=String.format("value is %f",54.15789); 
String f3=String.format("value is %54.15f",54.15789);
System.out.println(f1); 
System.out.println(f2); 
System.out.println(f3); 
  }
}

Output:

name is Nitish
value is 54.157890
value is 54.157890000000000

Java String format() Example 3

public class JavaStringFormatEx3 { 
    public static void main(String[] args) { 
        String s1 = String.format("%d", 101);          // Integer value
        String s2 = String.format("%f", 101.00);       // Float value
        String s3 = String.format("%x", 101);          // Hexadecimal value
        String s4 = String.format("%c", 'N');          // Char value 
        String s5 = String.format("%s", "Roy Nitish Nk"); // String value 
        System.out.println(s1); 
        System.out.println(s2); 
        System.out.println(s3); 
        System.out.println(s4); 
        System.out.println(s5); 
    } 
}

Output:

101101.
00000065
N
Roy Nitish Nk

Java String format() Example 4

public class JavaStringFormatEx4 { 
    public static void main(String[] args) {         
        String str1 = String.format("%d", 101); 
        String str2 = String.format("|%10d|", 101);  // Specifying length of integer 
        String str3 = String.format("|%-10d|", 101); // Left-justifying within the specified width 
        String str4 = String.format("|% d|", 101);  
        String str5 = String.format("|%010d|", 101); // Filling with zeroes 
        System.out.println(str1); 
        System.out.println(str2); 
        System.out.println(str3); 
        System.out.println(str4); 
        System.out.println(str5); 
    } 
}

Output:

101
101
101
101
0000000101

Related Topics

How to Convert int to double in Java

How to Convert int to double in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

Cosmic Superclass in Java

The parent class of all Java classes is the Object class. The Java Object class is the parent of all Java classes, whether directly or indirectly. The Object class is...

6 minutes read.

Java Math tan() Method

The tan() method of Java Math class returns the trigonometric tangent of the specified angle. Syntax: public static double tan(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The tan() method...

2 minutes read.

Thread Program in Java

Thread Program in Java Thread program in Java is the continuation of multithreading program in Java. In this topic, we will learn about the usage of threads, race condition in multithreading,...

8 minutes read.

Display List of TimeZone with GMT and UTC in Java

It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT. TimeZone Those...

5 minutes read.

How to Reverse a String in Java

How to Reverse a String in Java There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a...

6 minutes read.

Star Pattern Programs in Java

Star Pattern Programs in Java The star pattern programs in Java is the part of pattern programs in Java, which we discussed earlier. Right Triangle Star Pattern Filename: StarPatternExample.java public class StarPatternExample {              public static void...

4 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.

What’s New in Java 15

Sealed classes are the new concept that was introduced by Java 15. Sealed classes are a preview feature. Most of the features which are released in java 15 are in...

3 minutes read.

Java Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max. Syntax public static int max(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

How to Iterate a HashMap in Java?

Hash-map is a class of Java collections framework which has the functionality to implement the Map interface of Java. It stores the data in key-value pairs and can be accessed...

5 minutes read.

Java Math log10() Method

The log10() method of Math class returns the base 10 logarithmic value for the specified double argument. Syntax: public static double log10(double a) Parameters: The parameter ‘a’ represents a double value. Return Value: The log10() method...

2 minutes read.

JDBC vs ODBC

Difference Between JDBC and ODBC ODBC: ODBC (Open Database Connectivity) is the accepted method for accessing databases among organisations and programmers. A database is linked to other programmes, such as word processors, spreadsheets,...

4 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments. Syntax: public static String format(String format , Object… args) Parameter: locale : It specifies locale value to be applied on...

2 minutes read.

List all files in a Directory in Java

The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some...

3 minutes read.

Constructor in Java with Example

Java Constructor  The constructor is used for object initialization. It's a block of code that initializes a newly created object. It contains a collection of statements that are executed at the...

5 minutes read.

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.

History and Evolution of Java

Java is invented by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. Java is related to C++, which is inherited from...

3 minutes read.