Java String format() method
The Java String format() method is used for formatting strings. Similar to the printf() method, the format() method comes with a range of format specifiers and sub-specifiers that give developers a comprehensive option list to choose from when displaying data. Unlike the printf() method, the format() method output can be stored in a string variable for use in other parts of your application like a GUI text field or started for use later.
The Java String format() method uses a specified locale (n object which represents a specific geographical, political, or cultural region), format string, and object arguments. The local is not required, however the format string and object argument are required.
Datatype | Parameter | Description |
---|---|---|
Locale | l | Specifies which locale to use in the formatting. If it's null, the default Locale is used |
String | format | Defines the format of the Object (below) |
Object | args | The object that will be formatted based the format (above) |
There are two general syntax options for the format() method:
- String.format(locale, String format, Object… args);
- String.format(String format, Object… args);
Let’s break this down:
- String is the class that contains the format() method.
- format() is a static method that is called using the class name String.
- locale is optional and specifies a geographical location.
- String format is a string that is to be formatted.
- Object… args means more than one object can be passed to format().
Data from the additional args is formatted and written into placeholders marked by a % symbol. The argument formatting is dependent on the sequence of characters that follow the % symbol. The placeholders have the form:
%[arg$][flags][width][.precision]conversion
The components in [square brackets] are optional and the options are:
- arg$ is optional. A number followed by a $ sign which indicates which of the additional arguments to use, argument numbers start at 1. This can be replaced with a < which specifies that the argument from the previous placeholder should be used.
- flags is optional. A sequence of any of the following characters:
- – Makes the output left-justified by adding any padding spaces to the right instead of to the left.
- # Shows an alternate representation of the formatted data depending on the conversion.
- + Causes positive numbers to always be prefixed with “+”.
- (A space character) This prefixes a space to positive numbers, primarily so that the digits can be lined up with the digits of negative numbers.
- 0 Pads numbers with zeroes on the left.
- , Groups digits (for example by thousands) and puts separators between the groups. This is affected by the locale.
- ( Encloses negative numbers in parentheses.
- width is optional. A whole number specifying the minimum number of characters that the output should occupy. If necessary spaces are added to the right to reach this number, or to the left if the – flag is used.
- .precision is optional. A . followed by a whole number indicating how many decimal digits to show in the formatted data.
- conversion is required. A character which indicates how an argument’s data should be represented. If the character is uppercase the data will be formatted in uppercase where possible. The list of possible characters is shown in the table below.
Character | Conversion | Description |
---|---|---|
% | Percent | Displays a literal "%" character in the output. |
n | Line break | Displays a line break in the output. |
b or B | Boolean | Displays the boolean value of an argument as "true" or "false". If "B" is used then it displays "TRUE" or "FALSE" instead. |
h or H | Unsigned hexadecimal integer | Represents an argument's binary data as an unsigned hexadecimal integer. If "H" is used then digits A to F are shown in uppercase. Note: For any data other than positive integers this does not represent its real value. |
s or S | String | Displays the default string representation of the argument. If "S" is used then the string will be converted to uppercase where possible. |
c or C | Unicode character | Displays a unicode character representation of the argument. For whole numbers, this is the unicode character that corresponds to the number. If "C" is used then the character will be converted to uppercase where possible. |
d | Decimal integer | Represents a whole number as a decimal integer. |
o | Octal integer | Represents a whole number as an octal integer. The "#" flag will prefix the number with "0". |
x or X | Hexadecimal integer | integer Represents a whole number as a hexadecimal integer. The "#" flag will prefix the number with "0x". If "X" is used then digits A to F and the letter X are shown in uppercase. |
e or E | Scientific notation | Represents a floating point number in scientific notation. If "E" is used then the letter "E" of the representation will be uppercase. The "#" flag will force a decimal point even if there are no decimal digits. |
f | Floating point number | Represents a floating point number. The "#" flag will force a decimal point even if there are no decimal digits. |
g or G | General number | Displays the shortest representation between f and e or E for a floating point number. |
a or A | Hexadecimal floating point number | Display a floating point number's internal representation with hexadecimal digits. |
t or T | Time or date | Displays a formatted date or time. The t or T must be followed by one more character indicating how the date or time should be formatted. If "T" is used then text parts of a date or time such as "JANUARY" will be uppercase. The following characters can be used for date and time formatting: H - 24-hour format of an hour (00 to 23) I - 12-hour format of an hour (01 to 12) k - 24-hour format of an hour (0 to 23) l (lowercase 'L') - 12-hour format of an hour (1 to 12) M - Minutes with leading zeros (00 to 59) S - Seconds with leading zeros (00 to 59) (The value 60 may occur for leap seconds) L - Milliseconds with leading zeroes (000 to 999) N - Nanoseconds with leading zeroes (000000000 to 999999999) p - "am", "pm", "AM" or "PM" to indicate morning or afternoon z - Difference to Greenwich time (Example: -0800) Z - Timezone abbreviations (Examples: EST, MDT) s - The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) Q - The milliseconds since the Unix Epoch (January 1 1970 00:00:00 GMT) B - A full textual representation of a month (January through December) b or h - A short textual representation of a month (three letters) A - A full textual representation of a day (Example: Monday) a - A short textual representation of a day (Example: Mon) C - The first two digits of the year (For 1970, "19" would be shown) Y - A four digit representation of a year y - A two digit representation of a year j - The day of the year with leading zeroes (001 to 366) m - A numeric representation of a month (01 to 12) d - The day of the month (01 to 31) e - The day of the month without leading zeros (1 to 31) R - The time in 24-hour format (Example: 21:30) T - The time in 24-hour format with seconds (Example: 21:30:02) r - The time in 12-hour format with seconds (Example: 09:30:02 PM) ("AM" and "PM" are always uppercase) D - Date representation as month/day/year (Example: 12/17/23) F - Date representation as year-month-day (Example: 2023-12-17) c - Full date and time (Example: Thu Mar 28 10:51:00 EDT 2024) |
1. String.format(locale, String format, Object… args);
Use this option if you require formatting that has geographical, political, or cultural significance. For example, characters that represent a decimal differ around the world. In the US, UK and Ireland, a (decimal) point or dot is used; 123.99. In some European countries, a comma is used to represent a decimal: 123,99.
Use Java’s Locale to format an output to account for geographical differences. Note that If a locale is not passed to this method then the locale given by Locale.getDefault() is used.
String FR = String.format(Locale.FRANCE, "version is %f ",123.99); String US = String.format(Locale.US, "version is %f ",123.99); System.out.println("The FR " + FR); System.out.println("The US " + US);
Notice the difference in decimal character in the output below. The French decimal character is a comma whereas the US uses a point:
run: The FR version is 123,990000 The US version is 123.990000
We can see that the println() method uses a string concatenated with a variable which is a typical use of that method.
System.out.println(“The FR ” + FR);
The format() method is used to format a string that, in this case, we are assigning to a string variable.
String FR = String.format(Locale.FRANCE, “version is %f “,123.99);
This format() method has three parts:
- locale
- format
- argument
The first part is the locale and we have discussed that earlier. The second part contains a format specifier that will dictate how the third part will be formatted.
String.format(Locale.US, “version is %f “,123.99);
How does this work exactly:
- String.format has an argument contained within round brackets as follows:
- The locale will change anything that varies by region (for example the way most countries in Europe use a comma for decimals but the US, UK and Ireland use a full stop or period).
- A comma separates locale from the next part of the argument.
- The % character is a placeholder and acts like a flag that Java will pay attention to.
- The letter after the % character (in this case f for float) will determine how and what Java will place at that point in the outputted string.
- A comma separates the string from the next part of the argument
- Finally, the data (in this case 123.99) that will replace the % symbol and its character ends the argument.
This is a simple example, you can have more complex strings with multiple data pieces.
2. String.format(String format, Object… args);
Let’s leave Locale behind and focus on String format, Object… args. Refer back to the placeholder list and conversion table above. These will give you precise instructions to format virtually any output. Let’s illustrate this with a few examples:
Here we have three values: a string “euro”, a number 1900000.49662, and another string “Your balance is”.
The challenge is to format the number to a more readable format using commas to separate thousands and limit the decimal places to 2. We also need to rearrange the data to present the values in a more coherent way so that the output reads “Your balance is 1,900,000.50 euro”.
Here’s the solution:
String output = String.format("%3$s %2$,3.2f %1$s", "euro", 1900000.49662, "Your balance is"); System.out.println(output);
This is how each part of the placeholder %3$s %2$,3.2f %1$s works:
- 3$ indicates that the first argument should be presented last (“euro”)
- s indicates that the first argument is a string (“euro”)
- 2$ indicates that the value of the second argument is used next (1900000.4966)
- , indicates that digits should be grouped by thousands (1,900,000.4966)
- 3 indicates that the representation of the data should be at least 3 characters long
- .2 indicates that there should be two digits after the decimal point
- f indicates that the data is being represented as a floating point number
- 1$ indicates that the third argument should be presented first
- s indicates that the third argument is a string
The output is:
Run: Your balance is 1,900,000.50 euro BUILD SUCCESS
**INCOMPLETE 240611**