/**
Common format specifiers
-----------------------------------
%-10s // Left-aligned string, width 10
%10s // Right-aligned string, width 10
%5d // Integer, width 5
%08d // Integer, width 8, padded with zeros
%12.2f // Floating-point, width 12, 2 decimal places
*/
public class FixedWidthFields {
/**
* Formats a record using fixed-width fields.
*
* %-15s = left-aligned string with width 15
* %10d = right-aligned integer with width 10
* %12.2f = right-aligned decimal number with width 12
* and 2 digits after the decimal point
*/
public static String formatRecord(String name, int age, double salary) {
return String.format("%-15s%10d%12.2f",
name, age, salary);
}
public static void main(String[] args) {
// Store the formatted string in a variable
String result = formatRecord("Six", 60, 125000.0);
System.out.println(result);
}
}
/*
run:
Six 60 125000.00
*/