Skip to Main Content
Java™ Phrasebook
book

Java™ Phrasebook

by Timothy Fisher
November 2006
Intermediate to advanced content levelIntermediate to advanced
224 pages
3h 29m
English
Sams
Content preview from Java™ Phrasebook

Formatting Output

float hits=3;
											float ab=10;
											String formattedTxt =
											String.format("Batting average: %.3f", hits/ab);

In this phrase, we use the format() method to format an output string that prints a baseball batting average in the standard format of three decimal places. The batting average is defined as the number of hits, divided by the number of at-bats, ab. The format specifier %.3f tells the formatter to print the average as a floating point number with three digits following the decimal point.

JDK1.5 introduced the java.util.Formatter class, which can be used to easily format text. The Formatterclass works very similar to the printf function in the C language, and provides support for layout justification and alignment, common formats ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ruby Phrasebook

Ruby Phrasebook

Jason Clinton
Just Java™ 2

Just Java™ 2

Peter van der Linden

Publisher Resources

ISBN: 0768668255Purchase book