Java Formatter ( parentheses flag

PreviousNext

To show negative numeric output inside parentheses, rather than with a leading -, use the ( flag.

For example,

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

creates this string:

(100)
import java.util.Formatter;

public class Main {
   public static void main(String args[]) {
      Formatter fmt = new Formatter();

      fmt.format("%(d", -100);
      System.out.println(fmt);//   w   w  w .   d e   m o 2   s .    c o m  
      fmt.close();

      fmt = new Formatter();
      fmt.format("%(d", 100);
      System.out.println(fmt);
      fmt.close();

      fmt = new Formatter();
      fmt.format("%(d", -200);
      System.out.println(fmt);
      fmt.close();

      fmt = new Formatter();
      fmt.format("%(d", 200);
      System.out.println(fmt);
      fmt.close();
   }
}

Output

PreviousNext

Related