wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to avoid exponential in STRING to DOUBLE conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to avoid exponential in STRING to DOUBLE conversion" Watch "How to avoid exponential in STRING to DOUBLE conversion" New topic
Author

How to avoid exponential in STRING to DOUBLE conversion

Akhash Ramamurthy
Greenhorn

Joined: Nov 02, 2009
Posts: 25
1. I am reading a string value from a textField
2. Convert it into a Double using Double.valueOf(<textfield string t >).doubleValue();
3. My application is failing since the previous step returns a exponential value.

4. I tried DecimalFormat but this returns a string value which i cant use for further manipulation. All further manipulations are based on datatype DOUBLE of textfield string t.

5. Tried Big Decimal as well which returns a BigDecimal and which cant be type casted to double.

So please let me know how to convert string into double avoiding the exponential notation... I am sure my app just gets a max of 10 digits after the decimal point in the text field.




Software Engg
Akhash
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Akhash Ramamurthy wrote:// If i again convert this string into double the story repeats......


Primative doubles do not have formatting. It is *not* exponential nor non-exponential. It is simply a value.

The reason you are seeing exponential is when you are trying to print it -- which is done by converting to string. So... if you want a particular format, just convert it to a string prior to printing. Don't convert it back. Do all maths with the original double. Just convert it to string every time you want to print.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
class DoubleConversion wrote:BigDeci =12345.12345678908968693576753139495849609375
costbasis =12345,1234567891
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Akhash Ramamurthy wrote:

The first line already loses the precision. BigDecimal cannot get that precision back. If you need to maintain that precision skip the double completely:

And as a side note, instead of Double.valueOf(s).doubleValue() you can use Double.parseDouble(). That does not create the intermediate Double instance.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Akhash Ramamurthy
Greenhorn

Joined: Nov 02, 2009
Posts: 25
Primative doubles do not have formatting. It is *not* exponential nor non-exponential. It is simply a value.

The reason you are seeing exponential is when you are trying to print it -- which is done by converting to string. So... if you want a particular format, just convert it to a string prior to printing. Don't convert it back. Do all maths with the original double. Just convert it to string every time you want to print.

Henry


But my application fails whenever I see the exponential. In other cases it works completely fine. Just a reminder my application does all further manipulation based on the assumption that the value obtained from the textfield is a DOUBLE


Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Tell us how your application fails. If there's a stack trace, show us. Show us the code that's referenced in the topmost frame of the stacktrace, too.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to avoid exponential in STRING to DOUBLE conversion