In this post we will see String.format java examples.
Format method in String returns a formatted string using the specified format string and arguments.
1 |
public static String format(String format,Object... args) |
Params :
format – the format string
args – Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero.
The format specifiers for general, character, and numeric types have the following syntax:
%[argument_index$][flags][width][.precision]conversion
example:
String.format("%.2f", 100.22345) will return
100.22
Lets see some useful formatting types
1. Formatting String
%s – returns a String
%.2s – returns first 2 characters of the String
2. Formatting Integer
%d – returns a integer
3. Formatting Float
%f – returns float
%.3f – returns float with precision upto 3 decimal
%,2.3f – returns float with precision upto 3 decimal and separated by comma after 2 digits. (see example).
4. Formatting Date
%td – returns todays date
%tm – returns currenct month
%tY – returns current year in YYYY
%ty – returns current year in YY
String.format java example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.kscodes.sampleproject; import java.util.Date; public class StringFormatExample { public static void main(String[] args) { System.out.println("Format using %s ::: " + String.format("%s", "This is test String")); System.out.println("Format using %.2s ::: " + String.format("%.2s", "This is test String")); System.out.println("Format using %d ::: " + String.format("%d", 100)); System.out.println("Format using %f ::: " + String.format("%f", 54678.345267)); System.out.println("Format using %.3f ::: " + String.format("%.3f", 54678.345267)); System.out.println("Format using %,2.3f ::: " + String.format("%,2.3f", 54678.345267)); System.out.println("Format using %td %tm ::: " + String.format("%td %tm", new Date(), new Date())); System.out.println("Format using %tY %ty ::: " + String.format("%tY %ty", new Date(), new Date())); } } |
Output
1 2 3 4 5 6 7 8 |
Format using %s ::: This is test String Format using %.2s ::: Th Format using %d ::: 100 Format using %f ::: 54678.345267 Format using %.3f ::: 54678.345 Format using %,2.3f ::: 54,678.345 Format using %td %tm ::: 09 01 Format using %tY %ty ::: 2016 16 |
Execption in String.format
If a format is not convertible in the given type , then an exception is thrown
example:
If we try to convert a string to date we get the following exception
1 2 |
System.out.println("Format using %td ::: " + String.format("%td", "test String")); |
1 2 3 4 5 6 7 8 |
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source) at java.util.Formatter$FormatSpecifier.printDateTime(Unknown Source) at java.util.Formatter$FormatSpecifier.print(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.lang.String.format(Unknown Source) at com.kscodes.sampleproject.StringFormatExample.main(StringFormatExample.java:9) |
You can get more information on the format specifiers in the java docs – Here