This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: double amountOwed = cost + tax; // print amount owed with 2 places after the decimal point // allow a field of width 5 for the amount System.out.printf("Total owed: %5.2f", amountOwed); Output: Total owed: 4.88 Note: The width of the field is the total number of characters to be printed: • one space • the digit 4 • the decimal point • two digits after the decimal point Example: System.out.printf("%-7s%5.2f%n", "Tax:", tax); Now we have 3 format specifiers: • %-6s: s indicates a string, the hyphen is a flag which means left alignment, and %n is an end of line specifier...
View Full Document
- Spring '08
- printf, two digits, decimal point, Format Specifiers, PrintStream Class