For each double D, there is a range of decimal fractions that would convert to D. Double.toString with D as argument yields the member of that range with the fewest significant digits. Often, a "not too long" decimal fraction is the member of its range with the fewest significant digits, so it is the one you get on conversion to String.
For example:
import java.math.BigDecimal;
public class Test {
public static void main(String[] args) {
double d1 = 0.4;
double d2 = 0.400000000000000000001;
System.out.println(d1);
System.out.println(d2);
System.out.println(new BigDecimal(d1));
}
}
displays:
0.4
0.4
0.40000000000000002220446049250313080847263336181640625
Both "0.4" and "0.400000000000000000001" are in the range whose members convert to the double whose exact value is 0.40000000000000002220446049250313080847263336181640625. 0.4 is the shortest member of that range, so it is the result of the toString conversion. It is also a "not too long" decimal fraction.