Java PrintStream.printf()

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Overview

pw.printf("%s: %d", s, i);
System.out.printf("float: %2.2f\n",f);

Features

Leading Space Padding

This is useful when numbers with different lengths are required to be displayed aligned to the right.

Left-padding with spaces is achieved as follows:

System.out.printf("%widths", number)

The number will be converted to string and will be right justified, with the rest of the space willed with ' ', at the left.

Trailing Space Padding

System.out.printf("%-widths", something)

Hexadecimal Output

System.out.printf("%x", value)