English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Java String format() method returns a formatted string based on the parameters passed.
The syntax of the String format() method is:
String.format(String format, Object... args)
Note:
format() is a static method. We use the class name String to call the format() method.
In the above code, ... indicates that multiple objects can be passed to format().
format - Format string
args - 0 or more parameters
Returns the formatted string
class Main { public static void main(String[] args) { String language = "Java"; int number = 30; String result; // Format an object as a string result = String.format("Language: %s", language); System.out.println(result); // Language: Java //Format the number as a hexadecimal number result = String.format("Hexadecimal number: %x", number); // 1e System.out.println(result); // Hexadecimal number: 1e } }
In the above program, note the code
result = String.format("Language: %s", language);
This "Language: %s" is aFormat string.
Replace %s in the format string with the content of language. %s is a format specifier.
Similarly, %x is replaced with the hexadecimal value of number in String.format("Number: %x", number).
The following are commonly used format specifiers:
Specifier | Description |
---|---|
%b, %B | According to the parameter as "true" or "false" |
%s, %S | A string |
%c, %C | Unicode character |
%d | Decimal integer (only for integers) |
%o | Octal integer (only for integers) |
%x, %X | Hexadecimal integer (only for integers) |
%e, %E | Used for scientific notation (used for floating-point numbers) |
%f | Used for decimal numbers (used for floating-point numbers) |
class Main { public static void main(String[] args) { int n1 = 47; float n2 = 35.864f; double n3 = 44534345.76d; //Formatted as octal digits System.out.println(String.format("1In octal: %o", n1)); // 57 //Formatted as hexadecimal digits System.out.println(String.format("1In hexadecimal: %x", n1)); // 2f System.out.println(String.format("1In hexadecimal: %X", n1)); // 2F //Formatted as a string System.out.println(String.format("1As a string: %s", n1)); // 47 System.out.println(String.format("2As a string: %s", n2)); // 35.864 //Scientific notation format System.out.println(String.format("3Scientific notation: %g", n3)); // 4.45343e+07 } }
Output Result
n1Octal: 57 n1Hexadecimal: 2f n1Hexadecimal: 2F n1As a string: 47 n2As a string: 35.864 n3Scientific notation: 4.45343e+07
You can use multiple format specifiers in the format string.
//Using multiple format specifiers //Format string class Main { public static void main(String[] args) { int n1 = 47; String text = "Result"; System.out.println(String.format("%s\nHexadecimal: %x", text, n1)); } }
Output Result
Result Hexadecimal: 2f
Here, %%s is replaced with the value of text. Similarly, %%o is replaced with n1The hexadecimal value.
class Main { public static void main(String[] args) { float n1 = -452.534f; double n2 = -345.766d; //Format the floating-point number as is System.out.println(String.format("1 =%f", n1)); // -452.533997 System.out.println(String.format("2 =%f", n2)); // -345.766000 //Display to two decimal places System.out.println(String.format("1 =%.2f", n1)); // -452.53 System.out.println(String.format("2 =%.2f", n2)); // -345.77 } }
Output Result
n1 = -452.533997 n2 = -345.766000 n1 = -452.53 n2 = -345.77
Note:When we use %f to format-452.534When-452.533997. This is not because the format() method. Java does not return the exact representation of floating-point numbers.
Use %%.2When the f format specifier is used, format() gives two digits after the decimal point.
// using more than one format specifiers // in a format string class Main { public static void main(String[] args) { int n1 = 46, n2 = -46; String result; //Fill the number with spaces //The length of the string is5 result = String.format("|%5d|", n1); // | 46| System.out.println(result); //Fill the number with digits 0 //The length of the string is5 result = String.format("|%5d|", n1); // |00046| System.out.println(result); //Use a sign before the number result = String.format("%+d", n1); // +46 System.out.println(result); result = String.format("%+d", n2); // -46 System.out.println(result); //Enclose negative numbers in parentheses //and remove the sign result = String.format("%(d", n2); // (46) System.out.println(result); } }
//Use 0x before hexadecimal //Use 0 before octal class Main { public static void main(String[] args) { int n = 46; System.out.println(String.format("%#o", n)); // 056 System.out.println(String.format("%#x", n)); // 0x2e } }
If a specific language environment must be used, the String format() method also has another syntax.
String.format(Locale l, String format, Object... args)
//Use Locale import java.util.Locale; class Main { public static void main(String[] args) { int number = 8652145; String result; //Use the current locale result = String.format("Numbers: %,d", number); System.out.println(result); //Use GERMAN locale as the first parameter result = String.format(Locale.GERMAN, "German Numbers: %,d", number); System.out.println(result); } }
Output Result
Numbers: 8,652,145 German Numbers: 8.652.145
Note:In Germany, integers are represented by a period instead of a comma.