Add decimal_to_binary.py#4251
Conversation
|
It seems to me that this performs the same function as the built-in function "bin". |
Exactly, |
| @@ -0,0 +1,43 @@ | |||
| def decimal_to_binary(number: int) -> str: | |||
There was a problem hiding this comment.
I don't see any decimal number in the tests. Please provide tests for numbers like 13.321312312
| @@ -0,0 +1,43 @@ | |||
| def decimal_to_binary(number: int) -> str: | |||
There was a problem hiding this comment.
number need not be int, it can be float too since you have named your function as decimal_to_binary.
1 and 1.5 are both decimal numbers because 1 can also be written as 1.0 or 0.01 * 10**2.
if you are confused about decimal numbers check this Wikipedia link
| if number < 0: | ||
| number *= -1 | ||
| flag = 1 | ||
| answer = "" |
There was a problem hiding this comment.
Please write code that can actually convert decimal to binary, your code just converts int to binary. Even the build-in function bin can do that.
I have written a sample code to do so since I only have view permission, I want you to make some amendments.
a = ""
r = ""
count = 0
d = number - int(number)
number = int(number)
while(d and count<10):
d = d*2
r+=str(int(d))
if int(d)==d:
break
else:
d = d -int(d)
count+=1
while number:
a = str(number%2) + a
number = number >> 1
a = a+"."+r
print(a)|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
|
Please reopen this pull request once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to seek help from our Gitter or ping one of the reviewers. Thank you for your contributions! |
Describe your change:
I have added an algorithm to find the binary string representation of decimal numbers.
Checklist:
Fixes: #{$ISSUE_NO}.