Java Format Negative and Positive

Description

We can use + sign to add a + sign to positive numeric output.

Syntax

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

Example

Adding the + flag appends a + sign before positive numeric values.

For example,


import java.util.Formatter;
//from   ww w .jav  a 2 s  . c  om
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    // Right justify by default
    fmt.format("%+d", 100); 
    System.out.println(fmt);
  }
}

The output:





















Home »
  Java Tutorial »
    Data Format »




Java Formatter
Java Number Formatter