Sub topic 11
Formatting Text and Values
Formatting lets you place values into text in a set pattern. For example, you can make a price appear with two digits after the decimal point.
Java’s String.format method creates a formatted string. The System.out.printf method prints formatted text. Both use a format string: text that contains placeholders for values.
Format specifiers
A format specifier is a placeholder that starts with %. It tells Java how to display a value.
| Specifier | Use | Example value |
|---|---|---|
| `%s` | Text | `"Notebook"` |
| `%d` | A whole number | `3` |
| `%f` | A number with a decimal point | `2.5` |
| `%.2f` | A decimal number shown with two digits after the decimal point | `2.50` |
| `%n` | A new line | No value needed |
| `%%` | A percent sign | No value needed |
For %.2f, the .2 sets the number of digits after the decimal point. Java rounds the displayed result when needed. This does not change the original value.
Worked example: a receipt
Save this program as FormatReceipt.java:
import java.util.Locale;
public class FormatReceipt {
public static void main(String[] args) {
String item = "Notebook";
int quantity = 3;
double price = 2.5;
String receipt = String.format(
Locale.US,
"Item: %s%nQuantity: %d%nTotal: $%.2f",
item, quantity, quantity * price
);
System.out.println(receipt);
System.out.printf(Locale.US, "Unit price: $%.2f%n", price);
}
}Compile and run it:
javac FormatReceipt.java
java FormatReceiptThe output is:
Item: Notebook
Quantity: 3
Total: $7.50
Unit price: $2.50The values after the format string are called arguments. Java matches them to specifiers in order:
%susesitem.%dusesquantity.%.2fuses the result ofquantity * price.
The %n specifiers start new lines. They do not use arguments.
String.format returns the finished text, which the program saves in receipt. In contrast, System.out.printf prints the unit price directly.
Choose a locale
A locale is a setting for language and regional conventions. It can affect how formatted numbers appear. For example, some regions use a comma instead of a period as the decimal separator.
The example uses Locale.US so that the price uses a period. The import line makes Locale available to the program. Without an explicit locale, formatting uses the program’s default locale, so results may differ between systems.
Set a minimum width
You can add a number to a specifier to set its minimum width. For example, %8s displays text in a space at least eight characters wide. Shorter text gets spaces on the left.
With "Java" as its value, the format string |%8s| produces:
| Java|Use %-8s to put the extra spaces on the right. The width is a minimum: it does not cut off longer text.
Avoid common errors
Use a specifier that matches its value. For example, %d expects a whole number, not "three". Also provide enough arguments for the specifiers that need them. A mismatch or a missing argument causes a formatting error when the program runs.
Exercise
What does String.format(Locale.US, "%s: %d%%", "Score", 95) return?
Show answer
It returns "Score: 95%". The %% adds one percent sign and needs no argument.