So yeah I'm trying to figure a way on how to change a decimal into a binary in python
Asked
Active
Viewed 299 times
2 Answers
3
-
-
@karthikr Indeed, `bin` is only for integers, as stated in the documentation. However, I don't think the OP referred to `decimal.Decimal` – cnicutar Sep 11 '13 at 17:36
-
1
1
In [1]: def dec2bin(n):
...: if not n:
...: return ''
...: else:
...: return dec2bin(n/2) + str(n%2)
...:
In [2]: dec2bin(11)
Out[2]: '1011'

inspectorG4dget
- 110,290
- 27
- 149
- 241
-
Looks rather convoluted given that there are already standard functions to do just that. – arshajii Sep 11 '13 at 17:34
-
@arshajii: Yeah, but I figured I'd give a DIY answer for completeness, just in case that's what OP was after – inspectorG4dget Sep 11 '13 at 17:35