Align Printf Output in Java

Align printf output in Java

You can try the below example. Do use '-' before the width to ensure left indentation. By default they will be right indented; which may not suit your purpose.

System.out.printf("%2d. %-20s $%.2f%n",  i + 1, BOOK_TYPE[i], COST[i]);

Format String Syntax: http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

Formatting Numeric Print Output: https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

PS: This could go as a comment to DwB's answer, but i still don't have permissions to comment and so answering it.

Can't get printf to align properly

Try:

   System.out.println("");
System.out.printf("%1$-19s $%2$10.2f", "Sum of Deposits: ", (monthsToPay * monthDep));
System.out.println("");
System.out.printf("%1$-19s $%2$10.2f", "End of Year Total: ", allDepTotal);

The second literal is 19 characters wide. The - flag means left align and is not what you say you want for the numbers. You may have meant to use +.

I suggest you take a look at the formatter specification.

Left-align using printf right-aligns the first line of string

Actually the first line is left aligned, but if you want the first line to be like the rest, as you wrote, you should change the format flag.

    String str = "canada";
for (int i=0; i< str.length(); i++) {
System.out.printf("%20s", str.substring(i) + "\n");
}

align output with printf

%-10s will make the days take exactly 10 spaces, even if they are shorter.

for (int p = 0; p < days.length; p++) {
System.out.printf("%-10s%s%n", days[p], sales[p]);
}

Output

Monday    273.44
Tuesday 568.4

How to align String on console output

You can use format() to format your output according to your need..

    for(int i=1; i<13; i++){
for(int j=1; j<13; j++){

System.out.format("%5d", i * j);
}
System.out.println(); // To move to the next line.
}

Or, you can also use: -

System.out.print(String.format("%5d", i * j));

in place of System.out.format..

Here's is the explanation of how %5d works: -

  • First, since we are printing integer, we should use %d which is format specifier for integers..
  • 5 in %5d means the total width your output will take.. So, if your value is 5, it will be printed to cover 5 spaces like this: - ****5
  • %5d is used to align right.. For aligning left, you can use %-5d. For a value 5, this will print your output as: - 5****

Java Printf Alignment Output

You can see that periods take up less space than the other characters. You need to switch to a mono spaced font. Eclipse has some documentation for how to do that here.

You can see here that it works with a mono spaced font.

How to align all desired output to the left with printf in java

as to @Pshemo's comment, the dash alone isn't enough, a straightforward solution is to simpily convert the int into a String variable and concatenate a dot after that variable, then using it in the first field with a dash :

int number = 1;
String a = number + ".";

System.out.printf("%-8s | %-5s | %-15s\n", a, b, c);

Output :

1.       | 123   | PICO 

Java: Using printf() to BOTH left justify and right justify on SAME line

Please refer to Formatting Numeric Print Output

Try System.out.format(" %-10s%-10.2f%10.2f%n")



Related Topics



Leave a reply



Submit