formatting screen output how do i make my numbers look pretty? using system.out.printf();

Post on 01-Jan-2016

231 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Formatting Screen OutputHow 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!!!!

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

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.

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.

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

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

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

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

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

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%

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.

Wuzzles (for fun)

Wuzzle 1

Sugar- Gum

Answer:

Sugarless gum.

Wuzzle 2

Disc

Rim

Answer:

Discriminate.

Wuzzle 4

Rule

RulePlay

Answer:

Play by the rules.

Wuzzle 5

him get

Answer:

Get after him.

Wuzzle 6

ANOTHER 1

Answer:

One after another.

Wuzzle 7

0¢ BLIND 0¢

Answer:

Blind innocence (blind in no cents)

Unused (updated)

top related