Display numbers in scientific notation : DecimalFormat « I18N « Java Tutorial






import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {

  public static void main(String args[]) {
    NumberFormat formatter = new DecimalFormat();

    int maxinteger = Integer.MAX_VALUE;
    System.out.println(maxinteger); 
    formatter = new DecimalFormat("0.######E0");
    System.out.println(formatter.format(maxinteger));

    formatter = new DecimalFormat("0.#####E0");
    System.out.println(formatter.format(maxinteger));

    int mininteger = Integer.MIN_VALUE;
    System.out.println(mininteger); 

    formatter = new DecimalFormat("0.######E0");
    System.out.println(formatter.format(mininteger)); 

    formatter = new DecimalFormat("0.#####E0");
    System.out.println(formatter.format(mininteger));

    double d = 0.12345;
    formatter = new DecimalFormat("0.#####E0");
    System.out.println(formatter.format(d));

    formatter = new DecimalFormat("000000E0");
    System.out.println(formatter.format(d)); 
  }
}








13.13.DecimalFormat
13.13.1.new DecimalFormat('0.00')
13.13.2.new DecimalFormat('#.#') two digits
13.13.3.new DecimalFormat('#.######') more digits
13.13.4.new DecimalFormat('.######'), keep fraction
13.13.5.new DecimalFormat('#.000000'), append zero
13.13.6.new DecimalFormat('#,###,###'), group
13.13.7.Arabic DigitsArabic Digits
13.13.8.Format a number with leading zeros
13.13.9.The . symbol indicates the decimal point
13.13.10.The , symbol is used to group numbers
13.13.11.The ; symbol is used to specify an alternate pattern for negative values
13.13.12.The ' symbol is used to quote literal symbols
13.13.13.Use group separators and show trailing zeros
13.13.14.Display numbers in scientific notation
13.13.15.Displaying numbers with commas
13.13.16.new DecimalFormat('##00')
13.13.17.The 0 symbol shows a digit or 0 if no digit present