-1

I have a string having number as 12345686844.71 and I need to convert this into double so that I can get the same value as in string to use this in double value in formula.. This was tried by me , but this is not working and we are getting the value as 1.2345686844 .... actually it should be the value same as stored in string

double d = Double.parseDouble(string)
xenteros
  • 15,586
  • 12
  • 56
  • 91
Sandeep
  • 39
  • 2
  • Possible duplicate of [What is difference between Double.parseDouble(string) and Double.valueOf(string)?](http://stackoverflow.com/questions/10577610/what-is-difference-between-double-parsedoublestring-and-double-valueofstring) – Tim Biegeleisen Nov 09 '16 at 11:23

3 Answers3

1

What you have done is correct. You're printing the number in science notation.

Instead of System.out.print() use the following:

double d = Double.parseDouble(string)
System.out.printf("dexp: %f\n", d);

If you have pasted the whole value of your double you would see that there is E almost at the end of the printed value. It stands for multiplied by 10 to the power of and there is the exponent.

What's important isn't what is being printed but rather what's in the memory. The value stored in d is correct. The printed value might have many useless trailing zeroes.

You can also use the DecimalFormat class:

DecimalFormat df = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
System.out.println(df.format(d));
xenteros
  • 15,586
  • 12
  • 56
  • 91
-1

You can use BigDecimal class provided in java.

BigDecimal value = new BigDecimal(str);

ref: https://stackoverflow.com/a/3752626/7135460

Community
  • 1
  • 1
  • ok this is fine but I am trying to store the same value in double based formula for example : double z = x*(y/a) where x is double and y is double and a is big decimal – Sandeep Nov 09 '16 at 11:37
-3

double d = Double.parseDouble(string)

DevOpsGuY
  • 127
  • 12
  • This was tried by me , but this is not working and we are getting the value as 1.2345686844 .... actually it should be the value same as stored in string – Sandeep Nov 09 '16 at 11:24
  • 1
    Why did you copy/pasted a part of the question?? – xenteros Nov 09 '16 at 11:24