Reputation: 327
How to convert the "6020494385.89982"
string value to a double?
String ss = "6020494385.89982";
double dd = Double.parseDouble(String.valueOf(ss));
System.out.println(dd);
I am getting the output as
6.02049438589982E9
but I want 6020494385.89982
as double value.
Upvotes: 1
Views: 84
Reputation: 10987
You do not need to convert into double if in the end you want the representation in the same format as the current value of ss variable.
However if you need to convert for some other purpose and then need to show in that format later/else where you can use DecimalFormat as others have suggested.
Upvotes: 1
Reputation: 21985
Use of a DecimalFormat
object should do the trick
String ss = "6020494385.89982";
double dd = Double.parseDouble(String.valueOf(ss));
System.out.println(new DecimalFormat("#0.00000").format(dd));
Or you can use System.out#printf()
System.out.printf("%.5f", dd);
Because System.out.println
has a particular way of working with double precisions that will not work in this particular case for example.
Upvotes: 5
Reputation: 311723
The number is correct. 6.02049438589982E9
is just the scientific notation, which is the default when you print a double
. If you want the non-scientific notation, you could just use printf
instead of println
:
System.out.printf("%f\n", dd);
Upvotes: 2
Reputation: 670
6.02049438589982E9
is the same as 6020494385.89982
6.02049438589982E9
= 6.02049438589982 X 10^9
the value stored in the double is 6020494385.89982 However, when you print it out, it represents in this format: 6.02049438589982E9
Upvotes: 1