Java Data Type How to - Scientific notation format








Question

We would like to know how to scientific notation format.

Answer

 
import java.text.DecimalFormat;
//from  www .j  a  v  a2 s. c o  m
public class Main {
  public static void main(String[] argv) throws Exception {

    DecimalFormat formatter = new DecimalFormat("00E00");
    String s = formatter.format(-1234.567);
    System.out.println(s);
  
    formatter = new DecimalFormat("000E00");

    s = formatter.format(-1234.567); // -123E01
    System.out.println(s);

    formatter = new DecimalFormat("0000000000E0");
    s = formatter.format(-1234.567); // -1234567000E-6
    System.out.println(s);
  }
} 

The code above generates the following result.