I want to convert exponential to decimal. e.g. 1.234E3 to 1234.
+5
A:
It is not really a conversion, but about how you display the number. You can use NumberFormat to specify how the number should be displayed.
Check the difference:
double number = 100550000.75;
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(number);
System.out.println(formatter.format(number));
Bruno Rothgiesser
2009-12-01 13:05:58
Thanks a lot :)
sai
2009-12-02 08:54:04
can u tell me how to do the same for numbers that have decimal point. e.g. 1234567812.1212. The exponential notation will be 1.2345678121212E9. I need to bring it back to 1234567812.1212.If there are 4 numbers after decimal point , I need to get 4.If 5 number,then 5.
sai
2009-12-02 09:01:26
Sai, use #.########. You can add more # after the decimal separator to increase the precision. But note that the maximum precision will be determined by the data type that you use. If you use double, for example, you will not get more than 17 decimal places (just check what happens if you print 1/3 without a formatter). So, if you include more than seventeen # after the decimal separator, it won't increase the precision. If you need more precision than double, see the BigDecimal class.
Bruno Rothgiesser
2009-12-02 09:35:49
A:
You can do:
BigDecimal myNumber = new BigDecimal("1.234E3");
int myInt = myNumber.intValue();
// or
double myDouble = myNumber.doubleValue();
BTW: see here for what BigDecimal constructor will parse: http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal%28java.lang.String%29
David Roussel
2009-12-01 14:08:24