Today we are going to take a look at a fun little question and learn how to convert decimals to binary 🙂
First off, to clarify any confusion we have from the term “decimal”:
- We have the Decimal Number System – which is any number in base 10.
- We also have a Decimal Number – which is just a floating point number, for this we will use the more correct mathematical term rational numbers.
- Finally we have Decimal Part – which is the fractional part of a number, or the one after the floating point.
Now, as an accomplished software engineer you must know by now how to convert integers from base 10 to base 2, but what happens when we are trying to cover all rational numbers?
How to Convert Decimals To Binary – Brainstorm
Let’s think about how to approach this problem. Since we know how to convert the integer part, we should focus on how to convert the decimal part.
What does the decimal part of a number represent in binary. By analogy on the decimal number system:
- Decmal Number System – 0.101 = 1/101 + 0/102 +1/103
- Binary Number System – 0.101 = 1/21 + 0/22 +1/23
So to shift the floating point in binary we just need to multiply by 2. We can easily do this action in decimal too. And since we only have two cases for binary (0 or 1), we can infer that if the total value of our number multiplied by 2, is greater than 1, then we would have a 1 after the floating point in binary, otherwise a 0.
Let us start to write code for this:
How to Convert Decimals To Binary – Java
* Given a rational number in a String format, return its binary representation.
* @param sNumber - the number in base 10 representation.
* @return String - the binary representation of sNumber
public String convertDecimalToBinary(String sNumber)
StringBuffer sBinaryNumberWholePart = new StringBuffer(); // Using StringBuffer since we are going to add to the String often
StringBuffer sBinaryNumberDecimalPart = new StringBuffer(); // * Same as above.
int wholePart = Integer.parseInt(sNumber.substring(0, sNumber.indexOf('.')));
double decimalPart = Double.parseDouble(sNumber.substring(sNumber.indexOf('.'), sNumber.length()));
while (wholePart > 0)
sBinaryNumberWholePart.insert(0, (wholePart % 2));
wholePart >>= 1; // Division by 2 is the same as bit shift by 2 for unsigned whole numbers.
while (decimalPart > 0)
if (sBinaryNumberDecimalPart.length() > 32)
return "What the Heck? Im gonna overflow man. Conversion not possible";
decimalPart *= 2; // We cannot do bitshift for floating point numbers.
if (decimalPart >= 1)
decimalPart -= 1;
Check the notes on our how to convert decimals to binary implementation in Java:
- We used a StringBuffer instead of a String, since we continuously change the String and we do not want to create a new object every time.
- We used a bit-shift >>= 1, instead of a division by 2, since for most CPUs bit shifting is faster.
- We could not use bit-shifting for floating point numbers, since they have a different representation than integers.
Hope you enjoyed the writing, now you should know how to convert decimals to binary… and I’ll see you guys next time 🙂
Latest posts by Ajk (see all)
- Find Median from Numbers Array - January 1, 2018
- Find Whether a Number Can Be the Sum of Two Squares - August 12, 2017
- How to Find Two Primes Whose Sum is Equal to a Number N - April 22, 2017