DecimalFormat("##E0") in Java


DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. Let us set DecimalFormat("##E0") and use the format() method as well.

DecimalFormat decFormat = new DecimalFormat("##E0");
System.out.println(decFormat.format(-189.8787));
System.out.println(decFormat.format(8.19));
System.out.println(decFormat.format(9897.88));

Since, we have used DecimalFormat class in Java, therefore importing the following package is a must −

import java.text.DecimalFormat;

The following is the complete example −

Example

 Live Demo

import java.text.DecimalFormat;
public class Demo {
   public static void main(String[] argv) throws Exception {
      DecimalFormat decFormat = new DecimalFormat("##E0");
      System.out.println(decFormat.format(-189.8787));
      System.out.println(decFormat.format(8.19));
      System.out.println(decFormat.format(9897.88));
      System.out.println(decFormat.format(1.9099));
      System.out.println(decFormat.format(12.788));
      System.out.println(decFormat.format(9.678));
   }
}

Output

-1.9E2
8.2E0
99E2
1.9E0
13E0
9.7E0
raja
Published on 21-Dec-2018 11:09:52
Advertisements