Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# How to avoid exponential in STRING to DOUBLE conversion

Akhash Ramamurthy
Greenhorn
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.

Henry Wong
author
Marshal
Posts: 21510
84
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

Jonas Isberg
Ranch Hand
Posts: 118
class DoubleConversion wrote:BigDeci =12345.12345678908968693576753139495849609375
costbasis =12345,1234567891

Rob Spoor
Sheriff
Posts: 20669
65
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.

Akhash Ramamurthy
Greenhorn
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
Posts: 24212
35
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.