Use 0 symbol to show a digit or 0 if no digit present in Java

Description

The following code shows how to use 0 symbol to show a digit or 0 if no digit present.

Example


/*ww  w .j av  a 2  s . c  o m*/
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
  public static void main(String[] argv) throws Exception {

    NumberFormat formatter = new DecimalFormat("000000");
    String s = formatter.format(-1234.567);
    System.out.println(s);
    // number was rounded up

  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Data Format »




Java Formatter
Java Number Formatter