Java Format Line up Space

Description

We can use space to add a space before positive numeric output.

Syntax

fmt.format("% d", -100);

Example

The following code shows how to use justify flags to align positive and negative values.


import java.util.Formatter;
//from   w  w w. j  av  a2s  . com
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("% d", -100); 
    System.out.println(fmt); 
 
    fmt = new Formatter(); 
    fmt.format("% d", 100); 
    System.out.println(fmt); 
 
    fmt = new Formatter(); 
    fmt.format("% d", -200); 
    System.out.println(fmt); 
 
    fmt = new Formatter(); 
    fmt.format("% d", 200); 
    System.out.println(fmt); 
  }
}

The output:





















Home »
  Java Tutorial »
    Data Format »




Java Formatter
Java Number Formatter