import java.util.*; public class PrintfDemo { public static void main(String args[]) { System.out.println("Here are some numeric values " + "in different formats.\n"); System.out.println("12345678901234567890123456789012345678901234567890"); System.out.printf("%-25s%15s\n\n", "This is a string","This is a 2nd string"); System.out.println("12345678901234567890123456789012345678901234567890"); System.out.printf("%25s%-15s\n", "This is a string","This is a 2nd string"); System.out.println("\n"); System.out.printf("Default floating-point format: %f\n", 1234567.123); System.out.printf("Floating-point with commas: %,f\n", 1234567.123); System.out.printf("Negative floating-point default: %,f\n", -1234567.123); System.out.printf("Negative floating-point option: %,(f\n", -1234567.123); System.out.println(); System.out.printf("Line-up positive and negative values:\n"); System.out.printf("% ,.2f\n% ,.2f\n", 1234567.123, -1234567.123); Formatter fmt = new Formatter(); // Right justify by default fmt.format("%10.2f", 123.123); System.out.println(fmt); // Now, left justify. fmt = new Formatter(); fmt.format("%-10.2f", 123.123); System.out.println(fmt); } }