Java PrintStream.printf(): Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * java Format")
 
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* Format string syntax https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax
* https://docs.oracle.com/javase/8/docs/api/java/io/PrintWriter.html#printf-java.lang.String-java.lang.Object...-
=Internal=
=Internal=


* [[java Format#Subjects|java Format]]
* [[java Format#Subjects|java Format]]
=Overview=
<pre>
pw.printf("%s: %d", s, i);
</pre>
<pre>
System.out.printf("float: %2.2f\n",f);
</pre>
=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("%<b>''width''</b>s", ''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("%-<b>''width''</b>s", ''something'')
==Hexadecimal Output==
System.out.printf("%x", value)

Latest revision as of 19:20, 12 July 2018

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)