Java Data Type How to - Format double with zeros on left and right side








Question

We would like to know how to format double with zeros on left and right side.

Answer

import java.text.DecimalFormat;
//  w  ww  . ja va2  s.  c om
public class Main {

  public static void main(String[] args) {
    double myVal = 12.456;
    DecimalFormat leftside = new DecimalFormat("000.0000000000");
    System.out.println(leftside.format(myVal));
  }

}

The code above generates the following result.