# Convert decimal to binary using python

Note again that a decimal value in binary is almost always a recurring sequence and that the exact decimal value of the actual binary sequence in the computer of finite length is not the same as the original decimal value.

In this draft, the task does not give any guidelines for handling precision. So we will use 99 places after the decimal point and trim any trailing zeros and the decimal point, for integer case. Also, since J does not have a "Decimal floating point number" data type, we will use a list of characters to represent a decimal or binary number this corresponds roughly with the relevant feature set of REXX which seems to have had a strong influence on this draft of this task , and use internal mantissa,exponent representations during the conversion.

Well, I am no expert in OCaml, and my code may seem a bit messy, but I actually took a rather naive aproach Anyway, the program seems to work, but the algorithm s can probably be improved. After reading the discussion, I took into account the suggestion that the program should perform conversions from any base to any other base.

The binary to number conversion is easy because it's supported by Racket. Racket only supports the number to binary conversion for integer numbers, so we multiply the original number by a power of two, to get all the binary digits, and then we manipulate the string to place the point in the correct place.

Bases up to 62 will just use decimal digits along with upper and lowercase Latin letters. This REXX program is a modified version of the original program which can handle any base no limit , and the original program did more extensive error checking.

You can use the division method. So take a number say Then repeat the mod, div process until your number is 0. This will leave you with a list of 1's and 0's in the case of All you have to do now is reverse that number to get the correct binary number: From binary to decimal: This is slightly easier.

For example take binary: Hope you understand how binary and decimal conversions work in theory, so all you need to do now is convert the text explanation above into code. Good luck and have fun.

I'm afraid I don't know how I can help you any more without coding the whole program for you. I have already coded such a program in C.

I think once you understand what is required to code, you'll be able to work out what variable, etc. I can't just say: Here's my code from my C program for Decimal to Binary: The Binary to Decimal is a slight cheat because in C , you can convert a string to any given base e. Hope the code gives you some pointers as to what you might require.

Course Forum Section 2 Coding a program which converts binary in to de Coding a program which converts binary in to decimal and visa versa. Simon Urban over 4 years ago I'm afraid I don't know how I can help you any more without coding the whole program for you.