Java Data Type How to - Quote literal symbols in number format








Question

We would like to know how to quote literal symbols in number format.

Answer

When formatting a number we can use the single quote ' to quote a literal symbols.

import java.text.DecimalFormat;
import java.text.NumberFormat;
//from  w w  w .  java  2  s .c om
public class Main {
  public static void main(String[] argv) throws Exception {

    NumberFormat formatter = new DecimalFormat("'#'#");

    String s = formatter.format(-1234.567); 

    System.out.println(s);
  }
}

The code above generates the following result.