Skip to content

Latest commit

 

History

History
executable file
·
25 lines (24 loc) · 568 Bytes

Question5_2.md

File metadata and controls

executable file
·
25 lines (24 loc) · 568 Bytes

Question5_2

Solution

public class Question5_2 {
	public static String doubleToString(double d){
		if(d <= 0 || d >= 1) return "ERROR";
		StringBuilder binary = new StringBuilder();
		int counter = 0;
		binary.append('.');
		while(d != 0){
			counter++;
			if(counter > 32)	return "ERROR";
			int append = d * 2 >= 1 ? 1:0;
			binary.append(append);
			d = d * 2 - append;
		}
		return binary.toString();
	}
	public static void main(String[] args) {
		System.out.println(doubleToString(0.525d));
		System.out.println(Float.toHexString(0.25f));
	}
}