1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Solution { public: string printBin(double num) { int i = 0; string ret = "0."; while(num && i++ < 30) { num *= 2; if(num >= 1) { ret.append("1"); num -= 1; }else ret.append("0"); } if(num != 0) return "ERROR"; else return ret; } };
|