Format double with new DecimalFormat("0.#####E0") in Java

Java 8Object Oriented ProgrammingProgramming

Firstly, set the double values −

double d1 = 1.39876;
double d2 = 2.39876;
double d3 = 0.66920;

Now, format the double values with new DecimalFormat("0.#####E0") −

System.out.println(new DecimalFormat("0.#####E0").format(d1));
System.out.println(new DecimalFormat("0.#####E0").format(d2));
System.out.println(new DecimalFormat("0.#####E0").format(d3));

The following is an example −

Example

 Live Demo

import java.text.DecimalFormat;
public class Demo {
   public static void main(String[] argv) throws Exception {
      double d1 = 1.39876;
      double d2 = 2.39876;
      double d3 = 0.66920;
      System.out.println(new DecimalFormat("0.#####E0").format(d1));
      System.out.println(new DecimalFormat("0.#####E0").format(d2));
      System.out.println(new DecimalFormat("0.#####E0").format(d3));
   }
}

Output

1.39876E0
2.39876E0
6.692E-1
raja
Published on 21-Dec-2018 06:17:52
Advertisements