1

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]);
}
Arjit
  • 3,290
  • 1
  • 17
  • 18
user3461203
  • 13
  • 1
  • 3

1 Answers1

5

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

double d = 0.4;
System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(d)));
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525