Presentation is loading. Please wait.

Presentation is loading. Please wait.

Formatting Screen Output How do I make my numbers look pretty?

Similar presentations


Presentation on theme: "Formatting Screen Output How do I make my numbers look pretty?"— Presentation transcript:

1 Formatting Screen Output How do I make my numbers look pretty?
Using System.out.printf();

2 The Problem double dollars = 25000; double tax = dollars * 0.125;
System.out.println(“Income: $” + dollars); System.out.println(“Tax owed: $” + tax); Screen Output: Income: $ Tax owed: $ Verdict: UGLY!!!!

3 The Solution Replace these lines:
System.out.println(“Income: $” + dollars); System.out.println(“Tax owed: $” + tax); With these lines: System.out.printf(“Income: $%,.2f\n”, dollars); System.out.printf(“Tax owed: $%,.2f\n”, tax); Screen Output: Income: $25,000.00 Tax owed: $3,125.00 Verdict: Prettier

4 Examining the differences
Old code: System.out.println(“Income: $” + dollars); New code: System.out.printf(“Income: $%,.2f\n”, dollars); Differences between println and printf: The variable (dollars) comes at the end of the statement. The variable (dollars) is now separated by a comma. The variable is replaced with a mysterious format specifier %,.2f which IS located inside the double quotes.

5 What is a format specifier?
Sample format specifier: System.out.printf(“Income: $%,.2f\n”, dollars); Characteristics of the format specifier: It always begins with a % symbol It always ends with the letter f, d, or s Between the % and the final letter are some characters that specify what the format should look like.

6 What are the different final letters?
Sample format specifier: System.out.printf(“Income: $%,.2f\n”, dollars); System.out.printf(“Savings: $%,d\n”, savings); System.out.printf(“Last name: %s\n”, lastname); The format specifier always ends with the letter f, d, or s f means that the variable is a double d means that the variable is an int s means that the variable is a String

7 What are the characters in a format specifier?
Sample format specifier: System.out.printf(“Income: $%,.2f\n”, dollars); A comma (,) means the number should contain commas A number AFTER the decimal specifies how many digits should be to the right of the decimal place A number BEFORE the decimal specifies the total number of spaces that the number should take up on the screen

8 How do I right-justify my output?
Sample format specifier: System.out.println(“ ”); System.out.printf( “Tax: $%,15.2f\n”, tax); The format above means that the value in the variable tax will be right justified within 15 spaces. Sample Screen Output: Tax: $ ,125.00

9 How do I left-justify my output?
Sample format specifier: System.out.println(“ ”); System.out.printf( “Tax: $%,-15.2f\n”, tax); Add a negative sign in front of the integer to the left of the 15 above. The format means that the value in the variable tax will be right justified within 15 available spaces. The next item displayed will be AFTER after the 15th empty space. Sample Screen Output: Tax: $3,125.00

10 Multiple variable in the same line?
double mine = , yours = ; S.O.printf(“My money: $%,.2f\tYour money: $%,.2f\n", mine, yours); If you have multiple variables in the same print statement, separate each variable with commas at the end of the double quotes. The format specifiers stay within the double quotes. Sample Screen Output: My money: $ Your money: $1,200.20

11 What if I want to display a % to the screen?
Sample Screen Output: Tax rate: 8.25% Answer: You must include TWO % signs side by side to display ONE to the screen. (You have seen this before with / and //.) Sample format specifier: System.out.printf( “Tax rate: %.2f%%\n”,tax);

12 Congratulations! You are done.
Now, fill in the blank spaces in the Notes sheet and write the three required lines of code at the bottom of the Notes page.

13 Wuzzles (for fun)

14 Wuzzle 1 Sugar - Gum Sugarless Gum

15 Answer: Sugarless gum.

16 Wuzzle 2 Disc Rim Discriminate

17 Answer: Discriminate.

18 Wuzzle 4 Rule Play Play by the rules

19 Answer: Play by the rules.

20 Wuzzle 5 him get Get after him

21 Answer: Get after him.

22 Wuzzle 6 ANOTHER 1 One after another

23 Answer: One after another.

24 Wuzzle 7 0¢ BLIND 0¢ Blind Innocence

25 Answer: Blind innocence (blind in no cents)

26 Unused (updated)


Download ppt "Formatting Screen Output How do I make my numbers look pretty?"

Similar presentations


Ads by Google