Left justify output in Java

Java 8Object Oriented ProgrammingProgramming

Including a minus sign after the %, makes it left justified.

Note − By default, output is right justified

Firstly, create a formatter object −

Formatter f = new Formatter();

Now, use the format() method to left justify output −

f.format("|%-15.5f|", 299.675796)

The following is an example −

Example

 Live Demo

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      Formatter f = new Formatter();
      // left justify
      f = new Formatter();
      System.out.println(f.format("|%-15.5f|", 299.675796));
   }
}

Output

|299.67580 |
raja
Updated on 30-Jul-2019 22:30:24

Advertisements