• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to extract double value with the same format from String?  RSS feed

 
SunilK Chauhan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

i have one string including double value. Now i want to extract same value as double value.
i.e. String val = "0.0001";

Now when i am extracting this value as double, it changes it's format i.e. 1.0E-4
using code: double valAsDouble = new Double(val);

I also want to extract the same value as long and bigdecimal format with the same format.

Please suggest me that how to get it?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have String including double value. You have string of characters. Anyway. look for Double class in Java API, there is a method in it, which could achieve what you presumably want.
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean "it changes it's format"?
A double doesn't have a format.

If you are checking the value doing something like:

then that is simply displaying the double in the default format for your locale.
 
SunilK Chauhan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Dave.

I want it is displaying the default format of my Locale.

I want that If String has value like String val = "0.0001"; Than i want double should print value as 0.0001.

 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I would look into the printf() method (depending on where you are printing this value), and the formatting syntax linked to in that api.  That (or the DecimalFormat class) are the usual way of formatting output.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably cannot do what you are asking in a simple manner. For example:
original string:  .0001,  then convert into double (or Double), then later get back the string .0001
original string:  .00010,  then convert into double (or Double), then later get back the string .00010
original string: 1.E-4,  then convert into double (or Double), then later get back the string 1.E-4

The only way to do that is to save the original string, so that when you want it back as a String, use the saved String.
If you do some math on the value, and then want the result back as a String in the same format as the original, regardless of what that format was, then it is a very difficult problem indeed.

Wouldn't it be nice if NumberFormat had a constructor that took a String representation of a number and figured out the format, so it could be used later on on a different value!
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaaah!
I see now.
Yes, that one would be a bit trickier.

If we don't have to take into account 1.E-4, then I suppose we could count the digits before the decimal and after?

But, of course, that begs the question of "why"?
If the exact representation is important then maybe this isn't actually a number?
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One reason why would be that you want to see a specific number of significant digits. In the scientific world, the values 1.2 and 1.200 usually mean two different things - the first states that we know the value to be accurate to two significant digits, i.e. the value is in the range 1.15 < value < 1.25, while the latter indicates that we know it to be accurate to four significant digits, i.e., it is in the range 1.1995 < value < 1.2005
 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:One reason why would be that you want to see a specific number of significant digits. In the scientific world, the values 1.2 and 1.200 usually mean two different things - the first states that we know the value to be accurate to two significant digits, i.e. the value is in the range 1.15 < value < 1.25, while the latter indicates that we know it to be accurate to four significant digits, i.e., it is in the range 1.1995 < value < 1.2005

Another reason is wanting to output a column of right-justified numbers where the decimal point lines up.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in both of those cases you almost always decide in advance how many significant digits/digits after the decimal point you plan to show. The idea that you're going to do that based on the input is rather unusual, and I would be interested to see the use case which requires that.
 
Nikki Tha
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sunil,

Is this what you are looking for ?

String value="0.0001";
double d=(Double.valueOf(value));
System.out.format("%f",d);

 
SunilK Chauhan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Nikki, i am looking for the same format as i have given in input.

And also it is right that first we need to decide the format and decimal number inside of the digit given. But i think it is not possible all the time to predecide the decimal value as it is real time value generated by the server.
That's the issue that it changes it's format.

And i have to pass the value with same format for the further calculation which is not supported the format including 'E' which i am getting right now.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SunilK Chauhan wrote:That's the issue that it changes it's format.


It doesn't change its format. A double value doesn't have a format.

So let me try again to clarify. You are receiving a String which is, say "20.697" from some input. Then you want to parse that String to a double value and do something with it. Then you want to pass "20.697" to some other thing. Yes? Then like Fred already suggested, just keep the String "20.697" and pass it to that other thing.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!