Skip to content

Instantly share code, notes, and snippets.

@hilda8519
Created August 2, 2014 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hilda8519/7aeb3efcbc60786edc6c to your computer and use it in GitHub Desktop.
Save hilda8519/7aeb3efcbc60786edc6c to your computer and use it in GitHub Desktop.
public class Q52 {
public static void main(String[] args){
System.out.println(printBinary(0.111111));
System.out.println(printBinary(0.8));
}
static String printBinary(double num){
if(num>1 || num<0)
return "ERROR";
double remain = num;
StringBuffer buffer = new StringBuffer();
buffer.append(".");
while(remain>0){
if(buffer.length()>32){
return "ERROR";
}
if(remain >= 0.5){
buffer.append(1);
remain = remain-0.5;
}
else
buffer.append(0);
remain=remain*2;
}
return buffer.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment