Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to avoid exponential in STRING to DOUBLE conversion

 
Akhash Ramamurthy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21208
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class DoubleConversion wrote:BigDeci =12345.12345678908968693576753139495849609375
costbasis =12345,1234567891
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic