Skip to content

Instantly share code, notes, and snippets.

@jyhjuzi
Created July 25, 2014 05:05
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 jyhjuzi/7c30e0bd0a6b80e6bbef to your computer and use it in GitHub Desktop.
Save jyhjuzi/7c30e0bd0a6b80e6bbef to your computer and use it in GitHub Desktop.
package Chapter5;
public class Q5_2{
public static void main(String[] args){
System.out.println(printBinary(0.75));
System.out.println(printBinary(0.87948723));
}
static String printBinary(double input){
if(input>1 || input<0)
return "ERROR";
double remain = input;
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