Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a32aa9c

Browse files
complex problem with solution
1 parent 0a59e70 commit a32aa9c

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Python program to convert float
2+
# decimal to binary number
3+
4+
# Function returns octal representation
5+
def float_bin(number, places = 3):
6+
7+
# split() seperates whole number and decimal
8+
# part and stores it in two seperate variables
9+
whole, dec = str(number).split(".")
10+
11+
# Convert both whole number and decimal
12+
# part from string type to integer type
13+
whole = int(whole)
14+
dec = int (dec)
15+
16+
# Convert the whole number part to it's
17+
# respective binary form and remove the
18+
# "0b" from it.
19+
res = bin(whole).lstrip("0b") + "."
20+
21+
# Iterate the number of times, we want
22+
# the number of decimal places to be
23+
for x in range(places):
24+
25+
# Multiply the decimal value by 2
26+
# and seperate the whole number part
27+
# and decimal part
28+
whole, dec = str((decimal_converter(dec)) * 2).split(".")
29+
30+
# Convert the decimal part
31+
# to integer again
32+
dec = int(dec)
33+
34+
# Keep adding the integer parts
35+
# receive to the result variable
36+
res += whole
37+
38+
return res
39+
40+
# Function converts the value passed as
41+
# parameter to it's decimal representation
42+
def decimal_converter(num):
43+
while num > 1:
44+
num /= 10
45+
return num
46+
47+
# Driver Code
48+
49+
# Take the user input for
50+
# the floating point number
51+
n = input("Enter your floating point value : \n")
52+
53+
# Take user input for the number of
54+
# decimal places user want result as
55+
p = int(input("Enter the number of decimal places of the result : \n"))
56+
57+
print(float_bin(n, places = p))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /