Java String and Number Formatting with printf()
Java provides powerful formatting capabilities through the printf() method, which allows precise control
over how strings, numbers, and other data types are displayed. Formatting is especially useful for structured
output, reports, and aligning data properly.
This report explains:
• How to format strings and numbers
• How to align text and add leading zeros
• The different format specifiers available in Java
Understanding printf() Formatting
Basic Syntax
System.out.printf(format_string, values);
• format_string → Defines how the output should be formatted
• values → The actual values to be formatted
Example:
System.out.printf("Name: %s, Age: %d%n", "Rahul", 21);
Output:
Name: Rahul, Age: 21
Key Formatting Specifiers
1. String and Character Formatting
• %s → String (Example: "Java" → Java)
• %c → Character (Example: 'A' → A)
• %15s → Right-justified string (Example: "Java" → " Java")
• %-15s → Left-justified string (Example: "Java" → "Java ")
Example:
String s = "Java";
System.out.printf("'%15s'%n", s); // Right-justified
System.out.printf("'%-15s'%n", s); // Left-justified
Output:
' Java'
'Java '
2. Integer Formatting
• %d → Integer (Example: 42 → 42)
• %5d → Right-justified integer (Example: 42 → " 42")
• %-5d → Left-justified integer (Example: 42 → "42 ")
• %03d → 3-digit integer with leading zeros (Example: 7 → 007)
Example:
int num = 7;
System.out.printf("'%5d'%n", num); // Right-justified
System.out.printf("'%-5d'%n", num); // Left-justified
System.out.printf("'%03d'%n", num); // Leading zeros
Output:
' 7'
'7 '
'007'
3. Floating-Point Formatting
• %f → Decimal number (Example: 3.14 → 3.140000)
• %.2f → Decimal with 2 places (Example: 3.1415 → 3.14)
• %10.2f → Right-justified decimal with 2 places (Example: 3.14 → " 3.14")
• %-10.2f → Left-justified decimal with 2 places (Example: 3.14 → "3.14 ")
• %e → Scientific notation (Example: 1234.56 → 1.234560e+03)
Example:
double pi = 3.141592;
System.out.printf("%.2f%n", pi); // 2 decimal places
System.out.printf("%10.2f%n", pi); // Right-justified
System.out.printf("%-10.2f%n", pi); // Left-justified
System.out.printf("%e%n", pi); // Scientific notation
Output:
3.14
3.14
3.14
3.141592e+00
4. Boolean Formatting
• %b → Boolean (Example: true → true, false → false)
Example:
System.out.printf("%b%n", true);
System.out.printf("%b%n", false);
Output:
true
false
5. Hexadecimal, Octal, and Binary Formatting
• %x → Lowercase hexadecimal (Example: 255 → ff)
• %X → Uppercase hexadecimal (Example: 255 → FF)
• %o → Octal (Example: 8 → 10)
• %b → Binary is not supported in printf(), but can be done using
Integer.toBinaryString().
Example:
int num = 255;
System.out.printf("Hex (lowercase): %x%n", num);
System.out.printf("Hex (uppercase): %X%n", num);
System.out.printf("Octal: %o%n", num);
System.out.println("Binary: " + Integer.toBinaryString(num));
Output:
Hex (lowercase): ff
Hex (uppercase): FF
Octal: 377
Binary: 11111111
6. Date and Time Formatting
Java’s printf() doesn't directly support date formatting, but you can use Java’s java.time API.
Example using LocalDateTime
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateExample {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-
MM-dd HH:mm:ss");
System.out.printf("Current Date & Time: %s%n",
now.format(formatter));
}
}
Output (Example):
Current Date & Time: 2025-03-14 10:30:45
Combining Multiple Formatting Options
Example: Printing Name, Age, and Salary in a Structured Format
String name = "Rahul";
int age = 21;
double salary = 50000.75;
System.out.printf("%-15s %03d $%,.2f%n", name, age, salary);
Output:
Rahul 021 $50,000.75
• %-15s → Left-aligned name with 15 spaces
• %03d → 3-digit age with leading zeros
• $%,.2f → Salary formatted with commas and 2 decimal places
Use Cases of Formatting in Java
1. Tabular Output (Aligning data properly in console)
2. Fixed-Width Reports (Printing structured data with proper spacing)
3. Invoice & Ticket Printing (Consistent number formatting)
4. ID Formatting (e.g., 001, 002, ...)
5. Scientific Data Representation (Using %e for large numbers)
Java’s printf() method provides a powerful way to format strings and numbers for structured output. By
using left-justification, right-justification, leading zeros, decimal precision, and scientific notation, we can
format data in a professional and readable manner.