-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertintodecimal.py
81 lines (73 loc) · 2.34 KB
/
convertintodecimal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Convert other number systems into decimal
nr = input("Please Enter The Number to be Converted!:\t")
n = nr.split(".")
m = int(input("Please Enter Your Base of Number:\t"))
tot1 = 0
tot2 = 0
x = 0
if m != 16:
sentence = ""
print("\n***STEPS TO BE FOLLOWED:***\n")
print(f"{n[0]} = ", end="")
for i in (n[0][::-1]):
print(f"({i}X{m}**{x})", end=" + ")
sentence += f"{i}X{m ** x} + "
tot1 += int(i) * (m ** x)
x += 1
print(f"\n{n[0]} =", sentence)
print(f"For Integer Part:\n{n[0]} = {tot1}")
if len(n) == 2:
x = 1
print("\n******For Decimal Part******\n")
print(f"{n[1]} = ", end="")
sen = ""
for i in (n[1]):
print(f"({i}X{m}**{-x})", end=" + ")
sen += f"{i}X{1 / (m ** x)} + "
tot2 += int(i) * (m ** (-x))
x += 1
print(f"\n{n[1]} =", sen)
print(f"For Decimal Part:\n0.{n[1]} = {tot2}")
print("\nThe Final Number in Decimal Format:\t", tot1 + tot2)
for i in range(70):
print("*", end="")
else:
con = {'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15}
quo = []
rem = []
for i in n[0]:
if i in con.keys():
quo.append(str(con[i]))
else:
quo.append(i)
if len(n) == 2:
for i in n[1]:
if i in con.keys():
rem.append(str(con[i]))
else:
rem.append(i)
sentence = ""
print("\n***STEPS TO BE FOLLOWED:***\n")
print(f"{n[0]} = ", end="")
for i in (quo[::-1]):
print(f"({i}X{m}**{x})", end=" + ")
sentence += f"{i}X{m ** x} + "
tot1 += int(i) * (m ** x)
x += 1
print(f"\n{n[0]} =", sentence)
print(f"For Integer Part:\n{n[0]} = {tot1}")
if len(n) == 2:
x = 1
print("\n******For Decimal Part******\n")
print(f"{n[1]} = ", end="")
sen = ""
for i in rem:
print(f"({i}X{m}**{-x})", end=" + ")
sen += f"{i}X{1 / (m ** x)} + "
tot2 += int(i) * (m ** (-x))
x += 1
print(f"\n{n[1]} =", sen)
print(f"For Decimal Part:\n0.{n[1]} = {tot2}")
print("\nThe Final Number in Decimal Format:\t", tot1 + tot2)
for i in range(70):
print("*", end="")