String.format "%[argument number] [flags] [width] [.precision] type"
str = String.format("Today is %tD", new Date());
String s = String.format("%-12.5f%.20f", 12.23429837482,9.10212023134);
System.out.printf("%-12s%-12s%s\n","Column 1","Column 2","Column3");
if you don't want to print just want a formatted string for any other purpose String format() method is a way to go. In summary you can say that printf()writes on stdout while format() return you a formatted string.
Read full article from How to format String in Java – String format Example
%f: will print the number as it is.%15f: will pint the number as it is. If the number has less than 15 digits, the output will be padded on the left.%.8f: will print maximum 8 decimal digits of the number.%9.4f: will print maximum 4 decimal digits of the number. The output will occupy 9 characters at least. If the number of digits is not enough, it will be padded
String s = String.format("%-12.5f%.20f", 12.23429837482,9.10212023134);
System.out.printf("%-12s%-12s%s\n","Column 1","Column 2","Column3");
if you don't want to print just want a formatted string for any other purpose String format() method is a way to go. In summary you can say that printf()writes on stdout while format() return you a formatted string.
Read full article from How to format String in Java – String format Example
No comments:
Post a Comment