Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java output formatting

Tags:

java

format

I was wondering if Java has some sort of class to help on output formatting. I know in C++, in iomanip, there is a method call setw. I was wondering if Java has something similar to this.

like image 338
Steffan Harris Avatar asked Dec 04 '22 10:12

Steffan Harris


2 Answers

Have a look at java.util.Formatter.

String.format() provides a convenient wrapper.

For example (modified from an example on the link):

   String s = String.format("e = %+10.4f", Math.E);

It goes beyond C's ?printf formats. For example, it supports an optional locale, and format symbols can be associated with an argument by explicit index rather than implicit.

Edit: Fixed the link above.

like image 115
Andy Thomas Avatar answered Dec 05 '22 22:12

Andy Thomas


String.format(..) and possibly java.text.*

like image 37
Bozho Avatar answered Dec 05 '22 23:12

Bozho