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

Description

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

Example


/*w  w  w .j a v a 2 s.  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 »
    Java Data Type »




Java Boolean
Java Byte
Java Character
Java Currency
Java Double
Java Enum
Java Float
Java Integer
Java Long
Java Short
Java Auto Grow Array
Java Array Compare
Java Array Convert
Java Array Copy Clone
Java Array Fill
Java Array Search and Sort
Java String Convert
Java String File
Java String Format
Java String Operation
Java BigDecimal
Java BigInteger