MCPcopy Index your code
hub / github.com/careercup/ctci / printBinary

Method printBinary

java/Chapter 5/Question5_2/Question.java:4–26  ·  view source on GitHub ↗
(double num)

Source from the content-addressed store, hash-verified

2
3public class Question {
4 public static String printBinary(double num) {
5 if (num >= 1 || num <= 0) {
6 return "ERROR";
7 }
8
9 StringBuilder binary = new StringBuilder();
10 binary.append(".");
11 while (num > 0) {
12 /* Setting a limit on length: 32 characters */
13 if (binary.length() > 32) {
14 return "ERROR";
15 }
16 double r = num * 2;
17 if (r >= 1) {
18 binary.append(1);
19 num = r - 1;
20 } else {
21 binary.append(0);
22 num = r;
23 }
24 }
25 return binary.toString();
26 }
27
28 public static String printBinary2(double num) {
29 if (num >= 1 || num <= 0) {

Callers 1

mainMethod · 0.95

Calls 3

appendMethod · 0.80
lengthMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected