GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Python/Decimal_To_Binary.py at master · Sophie-Williams/Python

Expand file tree

/

Decimal_To_Binary.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
42 lines (30 loc) · 1.5 KB
/

Decimal_To_Binary.py

Copy path

File metadata and controls

42 lines (30 loc) · 1.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
'''
PYTHON 3
Author: Sandeep Pillai (www.github.com/Corruption13)
Program: Decimal to Binary converter.
THis program accepts fractional values, the accuracy can be set below:
'''
decimal_accuracy = 7
def dtbconverter(num): # Function inputs a float value and returns a list as output
# Reasoning for list instead of integer: to avoid integer overflow error.
whole= [] # The part before decimal point
fractional = ['.'] # The part after decimal point
decimal = round(num%1, decimal_accuracy) # Extract fractional number part of decimal
w_num = int(num) # Extract whole number part of decimal.
i=0 # Some fractional decimal numbers have infinite binary values, so we limit this loop below.
#Loop to find binary of decimal part
while(decimal!=1 and i<decimal_accuracy):
decimal = decimal*2
fractional.append(int(decimal//1))
decimal = round(decimal%1, decimal_accuracy)
if(decimal == 0): break # Removes trailing zeros.
i = i + 1
#Loop to find binary of whole number part.
while(w_num!=0):
whole.append(w_num%2)
w_num = w_num//2
whole.reverse()
return whole + fractional ### End of dtbconverter() - 16 lines
#Test lines.
number = float(input("Enter ANY base-10 Number: "))
print("The Binary Equivalant: " , *dtbconverter(number))
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.