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

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

Embed Size (px)

Citation preview

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

Formatting Screen OutputHow do I make my numbers look pretty?

Using System.out.printf();

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

The Problemdouble dollars = 25000;

double tax = dollars * 0.125;

System.out.println(“Income: $” + dollars);

System.out.println(“Tax owed: $” + tax);

Screen Output:Income: $25000.0Tax owed: $3125.00000000079

Verdict: UGLY!!!!

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

The SolutionReplace 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);

Verdict: PrettierScreen Output:Income: $25,000.00Tax owed: $3,125.00

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

Examining the differencesOld code:

System.out.println(“Income: $” + dollars);

New code:System.out.printf(“Income: $%,.2f\n”, dollars);

Differences between println and printf:1. The variable (dollars) comes at the end of the statement.2. The variable (dollars) is now separated by a comma.3. The variable is replaced with a mysterious format specifier%,.2f which IS located inside the double quotes.

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

What is a format specifier?Sample format specifier:System.out.printf(“Income: $%,.2f\n”, dollars);

Characteristics of the format specifier:1. It always begins with a % symbol

2. It always ends with the letter f, d, or s

3. Between the % and the final letter are some characters that specify what the format should look like.

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

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

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

What are the characters in a format specifier?Sample format specifier:System.out.printf(“Income: $%,.2f\n”, dollars);

1. A comma (,) means the number should contain commas

2. A number AFTER the decimal specifies how many digits should be to the right of the decimal place

3. A number BEFORE the decimal specifies the total number of spaces that the number should take up on the screen

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

How do I right-justify my output?Sample format specifier:

System.out.println(“ 123456789012345”);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: 123456789012345

Tax: $ 3,125.00

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

How do I left-justify my output?Sample format specifier:

System.out.println(“ 123456789012345”);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: 123456789012345

Tax: $3,125.00

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

Multiple variable in the same line?double mine = 135.12, yours = 1200.2;

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: $135.12 Your money: $1,200.20

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

What if I want to display a % to the screen?

Sample format specifier:System.out.printf( “Tax rate: %.2f%%\n”,tax);

Answer: You must include TWO % signs side by side to display ONE to the screen. (You have seen this before with / and //.)

Sample Screen Output:Tax rate: 8.25%

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

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.

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

Wuzzles (for fun)

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

Wuzzle 1

Sugar- Gum

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

Answer:

Sugarless gum.

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

Wuzzle 2

Disc

Rim

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

Answer:

Discriminate.

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

Wuzzle 4

Rule

RulePlay

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

Answer:

Play by the rules.

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

Wuzzle 5

him get

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

Answer:

Get after him.

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

Wuzzle 6

ANOTHER 1

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

Answer:

One after another.

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

Wuzzle 7

0¢ BLIND 0¢

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

Answer:

Blind innocence (blind in no cents)

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

Unused (updated)