Question

i search in this site and other for converting double value like .4 to binary but there is no useful answers ! i know the procedure multiply by 2 if the number is bigger than 1 save the 1 subtract it and more time multiply it by 2 and so on . help me !

double d = .4 ; 
double e ;
while ()
{
e = d*2; 
if (e>1) {
s[i]= 1;
e=d-1;
} else {
s[i] = 0;
}
System.out.print(s[i]);
}
Was it helpful?

Solution

Use Double.doubleToRawLongBits(double), and then Long.toBinaryString(long) on the result:

double d = 0.4;
System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(d)));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top