© Doug Winnie 2021
D. WinnieEssential Java for AP CompScihttps://doi.org/10.1007/978-1-4842-6183-5_12

12. Output Formatting

Doug Winnie1  
(1)
Mission Hills, KS, USA
 

What a value is and how we display it are two different things. We can adapt how we display various value literals through formatters that convert and display values in different ways.

To do this, we will use the printf() method that will contain the information we want to display, but includes special codes that change the way the values are shown on the screen.

Decimal Formatters

The most basic is when we work with decimals. Sometimes, we need to display more digits in a number than are needed.

Here’s the code for how this works.
System.out.println(2.3f);
System.out.printf("%.2f",2.3f); ...

Get Essential Java for AP CompSci: From Programming to Computer Science now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.