Format integer values with java.text.DecimalFormat

Java 8Object Oriented ProgrammingProgramming

Here, we are using the DecimalFormat class in Java to format integer value, which is the java.text.DecimalFormat package.

Let us take the following format: DecimalFormat("0.######E0") and use the format() method for this purpose −

new DecimalFormat("0.#####E0").format(5089)
new DecimalFormat("0.#####E0").format(608)

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

import java.text.DecimalFormat;

The following is an example −

Example

 Live Demo

import java.text.DecimalFormat;
public class Demo {
   public static void main(String[] argv) throws Exception {
      System.out.println(new DecimalFormat("0.#####E0").format(387979));
      System.out.println(new DecimalFormat("0.#####E0").format(29797));
      System.out.println(new DecimalFormat("0.#####E0").format(49788));
      System.out.println(new DecimalFormat("0.#####E0").format(5089));
      System.out.println(new DecimalFormat("0.#####E0").format(608));
      System.out.println(new DecimalFormat("0.#####E0").format(20));
   }
}

Output

3.87979E5
2.9797E4
4.9788E4
5.089E3
6.08E2
2E1
raja
Published on 21-Dec-2018 06:24:10
Advertisements