Created
July 25, 2014 05:05
-
-
Save jyhjuzi/7c30e0bd0a6b80e6bbef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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