Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Converting string to double always gives me wrong representation

I want to convert this string "0.00071942446044" to double by using Double.parsedouble method but it always gives this answer 7.1942446044E-4

Is there any idea to convert it to double but keeping the same number as it is in the string?

like image 490
Monjed Seder Avatar asked Mar 24 '23 22:03

Monjed Seder

2 Answers

Although both numbers are exactly the same, you could use DecimalFormat to manipulate the format in a way you like, only for presentation purpose. Here is an example:

String s = "0.00071942446044";

Double d = Double.parseDouble(s);
DecimalFormat df = new DecimalFormat("#.##############");

System.out.println("double: " + d);
System.out.println("formatted: " + df.format(d)); 

The out is:

double: 7.1942446044E-4
formatted: 0.00071942446044

Note that the number of # after decimal point is exactly the same as your example.

like image 198
Armaiti Avatar answered Apr 24 '23 22:04


You can use new BigDecimal(myString), this is not the same but will keep the same representation. It provides API for doing different math, but is slower than doing arithmetical operations with doubles.

like image 37
hoaz Avatar answered Apr 24 '23 23:04