created 01/12/2020

go to home page   go to next page highlighting

CHAPTER 15 — Formatted Output with printf()

This chapter shows how to use System.out.printf() to format values. This is an alternative to the formatting methods of the previous chapter.

With printf() you have control over how values are printed. You can ask for this:

System.out.printf("%8.2f%n", 40/3.0);

13.33

The "%8.2f" says to use 8 spaces with two of them for the decimal fraction. The "%n" means newline. Without formatting you get

System.out.println(40.0/3.0);

13.333333333333334

This chapter is optional. Future chapters do not depend on it, so you can safely skip it. However, your programs will produce better looking output if you use number formatting.

The material in this chapter is not tested in AP Computer Science.

Chapter Topics:


QUESTION 1:

How would you like to see this number displayed:

Math.PI

Some choices are:


go to home page   go to next page