×

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

Java Boolean getBoolean() Method

The getBoolean() method of Java Boolean class returns true if the specified system property is not null and is equal to the String ‘true’, else the result returned is false. Syntax public...

1 minute read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

Convert list to array Java

One of the popular collection interfaces for storing an ordered collection is the List. The List interface may contain repeating groups and preserves the insertion order of entries. This article will...

4 minutes read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

Minimum Number of Taps to Open to Water a Garden in Java

Problem Statement The issue is that a gardener wants to water a (single-dimensional) garden using the fewest possible tap openings. The goal is to determine the minimum necessary taps to be...

8 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

FizzBuzz Program in Java

FizzBuzz is a well-known children's game. This game helps children learn division. The FizzBuzz game is becoming a popular programming question, appearing frequently in Core Java interviews. This section will...

3 minutes read.

Java Math floorMod() Method

The floorMod() method of Math class returns the floor modulus of the specified arguments. It firstly divides the dividend and divisor and then returns an integer that is equal to...

1 minute read.

Access modifiers in Java

Access modifiers in Java with Example In Java, there are two types of access modifiers one is a non-access modifier, and other is access modifier. If we talk about access modifier, there...

3 minutes read.

Sphenic Number in Java

In this section, we will learn what is a sphenic number is and show you how to write Java programmes to determine if a specific number are sphenic or not....

3 minutes read.

Davis Staircase Problem in Java

Davis has several stairs in his home and prefers to ascend one, two, or three steps at a time. As a highly clever youngster, he thinks about how many ways...

3 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

Java RandomAccessfile

Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer...

3 minutes read.

How to install Java in Windows 10

To make programs that can run on our systems, we need to install the programming language related software in our systems. Different programming language requires a different type of software aka...

6 minutes read.

Rehashing in Java

The most crucial idea mostly in the data structure is hashing, which is utilized to change a particular key into some other value. The hash function can be used to...

7 minutes read.

Convert List to String in Java

We occasionally need to create a string from a list of characters. Since a string is only a collection of characters, a character array can be converted into a string....

6 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.