Left justify output in Java


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 |

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements