Format number with ###E0 (exponent must be multiple of 3) in Java

Description

The following code shows how to format number with ###E0 (exponent must be multiple of 3).

Example


//from w  w w .j a  v a2s .co  m
import java.text.DecimalFormat;

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

    DecimalFormat formatter = new DecimalFormat("###E0"); 
    String s = formatter.format(-1234.567); // -1.23E3
    System.out.println(s);
    s = formatter.format(-123.4567); // -123E0
    System.out.println(s);
    s = formatter.format(-12.34567); // -12.3E0
    System.out.println(s);
    s = formatter.format(-1.234567); // -12.3E0
    System.out.println(s);
    s = formatter.format(-.1234567); // -123E-3
    System.out.println(s);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Data Format »




Java Formatter
Java Number Formatter