MCPcopy Create free account
hub / github.com/geekcomputers/Python / dtbconverter

Function dtbconverter

Decimal_To_Binary.py:5–33  ·  view source on GitHub ↗
(num)

Source from the content-addressed store, hash-verified

3
4
5def dtbconverter(num):
6 whole = []
7 fractional = ["."]
8
9 decimal = round(num % 1, decimal_accuracy)
10 w_num = int(num)
11
12 i = 0
13 while decimal != 1 and i < decimal_accuracy:
14 decimal = decimal * 2
15 fractional.append(int(decimal // 1))
16 decimal = round(decimal % 1, decimal_accuracy)
17 if decimal == 0:
18 break
19 i += 1
20
21 while w_num != 0:
22 whole.append(w_num % 2)
23 w_num = w_num // 2
24 whole.reverse()
25
26 i = 0
27 while i < len(whole):
28 print(whole[i], end="")
29 i += 1
30 i = 0
31 while i < len(fractional):
32 print(fractional[i], end="")
33 i += 1
34
35
36number = float(input("Enter Any base-10 Number: "))

Callers 1

Calls 2

appendMethod · 0.45
reverseMethod · 0.45

Tested by

no test coverage detected