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/10^{1}+ 0/10^{2}+1/10^{3}**Binary Number System**– 0.101 = 1/2^{1}+ 0/2^{2}+1/2^{3}

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

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 |
/** * 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) { sBinaryNumberDecimalPart.append(1); decimalPart -= 1; } else { sBinaryNumberDecimalPart.append(0); } } return sBinaryNumberWholePart.append(".").append(sBinaryNumberDecimalPart).toString(); } |

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