We would like to know how to set the multiple of the exponent.
The number of #'s to the left of the decimal point sets the multiple of the exponent.
import java.text.DecimalFormat; /*from w w w.j av a 2 s . co m*/ public class Main { public static void main(String[] argv) throws Exception { DecimalFormat formatter = new DecimalFormat("#E0"); // exponent can be any // value String s = formatter.format(-1234.567); System.out.println(s); s = formatter.format(-.1234567); System.out.println(s); } }
The code above generates the following result.
DecimalFormat("##E0") (exponent must be multiple of 2)
import java.text.DecimalFormat; /*from w w w.j av a 2 s .c om*/ public class Main { public static void main(String[] argv) throws Exception { DecimalFormat formatter = new DecimalFormat("##E0"); String s = formatter.format(-1234.567); System.out.println(s); s = formatter.format(-123.4567); System.out.println(s); s = formatter.format(-12.34567); System.out.println(s); } }
The code above generates the following result.
DecimalFormat("###E0") (exponent must be multiple of 3)
import java.text.DecimalFormat; // w ww . jav a 2s .c o m 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); } }