Возможно, вы захотите обработать целую и дробную часть:
public String toBinary(double d, int precision) {
long wholePart = (long) d;
return wholeToBinary(wholePart) + '.' + fractionalToBinary(d - wholePart, precision);
}
private String wholeToBinary(long l) {
return Long.toBinaryString(l);
}
private String fractionalToBinary(double num, int precision) {
StringBuilder binary = new StringBuilder();
while (num > 0 && binary.length() < precision) {
double r = num * 2;
if (r >= 1) {
binary.append(1);
num = r - 1;
} else {
binary.append(0);
num = r;
}
}
return binary.toString();
}