-3

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?

  • `double`s don't have a format. What you're describing is the result of the round-trip `String->double->String`. It's imprecise. – user207421 Feb 22 '17 at 10:01
  • 1.0E-4 is the same as 0.0001 but different notation – cralfaro Feb 22 '17 at 10:01
  • What is your problem? whether you use 1.0E-4 * 10000 or you use 0.0001 * 10000 the result is always going to be the same. – Blip Feb 22 '17 at 14:57

1 Answers1

1

The double is just a Java representation, the format does not matter at all.

When you are going to print this number and you need a particular layout you can easily change it using String.format.

Here you can find the documentation about it.

For example maybe in your case this could be a good solution

String myFormattedRepresentation = String.format("%1$,.2f", myDouble);
rakwaht
  • 3,666
  • 3
  • 28
  • 45