Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

Java String format Method

The Java String format method is one of the String Methods, which is to return a formatted string using the user-specified format and arguments. In this article, we will show how to write a Java String format method with an example. The basic syntax of the string.format is as shown below.

Java String.format syntax

The Java Programming Language provides two different methods to format the given string. The following method accepts two parameters: the first parameter is the format we want to apply, and the second is the arguments referenced by the format specifier.

public static String format (String format, Object.. args);  // It will return String as output 

//In order to use in program
String.format (String format, Object.. args);

The following Java string.format method accepts three parameters: first parameter is the locale you want to apply, second parameter is the format you want to apply. The third argument is the args referenced by a format specifier.

public static String format (locale l, String format, Object.. args);  // It will return String as output 

//In order to use in program
String.format (locale l, String format, Object.. args);
  • Locale: This is the locale we want to apply during the formatting. If the locale is NULL, then no localization is applied.
  • Format: Please specify the format string. For example, %d, %f, %s etc.
  • Args: Arguments referenced by the format specifier in the Format. If we specify more arguments than the format specifier, the extra arguments ignored.

IllegalFormatException: If the format string contains an illegal syntax, then the format method will throw this error. The illegal syntax may include:

  • When the format specifier is incompatible with the given arguments.
  • Or when we specify insufficient arguments.

NullPointerException: If the format is NULL.

TIP: If we omit Locale argument, then String.format will take the system default localization.

Java String.format Example

In this program, we use the String.format method to format the given object using the user-specified format.

// Java String format method example
package StringFunctions;
import java.util.Date;
import java.util.Locale;

public class FormatMethod {

	public static void main(String[] args) {
		double dbl = 98765.457896;
		
		String str1 = String.format("%f", dbl);
		System.out.println(str1);
		
		String str2 = String.format("%,2.4f", dbl);
		System.out.println(str2);
		
		String str3 = String.format("%d", 9876);
		System.out.println(str3);
		
		String str4 = String.format("-%d", 2406);
		System.out.println(str4);
		
		String str5 = String.format("Today is %tD", new Date());
		System.out.println(str5);
		
		String str6 = String.format("%s", 240.9876);
		System.out.println(str6);
		
		String str = "Tutorial";
		String std = String.format(Locale.ENGLISH, "%s Gateway",str);
		System.out.println(std);
	}
}
Java String.format Method 1

Within this example program, the following string.format statement calls the public static String format (String format, Object.. args) method to format the user-specified double value to float.

String str1 = String.format("%f", dbl);

The following format String Function code will format 9876 value to a positive integer.

String str3 = String.format("%d", 9876);

The below statement will format 2406 value to Negative integer.

String str4 = String.format("-%d", 2406);

The following Java string.format statement formats 240.9876 value to String data.

String str6 = String.format("%s", 240.9876);

The following statement calls public static String format (locale l, String format, Object.. args) method to format string data using the specified local (i.e., ENGLISH).

String std = String.format(Locale.ENGLISH, "%s Gateway",str);

Filed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy