Given an fraction decimal number n and integer k, convert decimal number n into equivalent binary number up-to k precision after decimal point.

Input: n = 2.47, k = 5 Output: 10.01111 Input: n = 6.986 k = 8 Output: 110.11111100

**A) Convert the integral part of decimal to binary equivalent**

- Divide the decimal number by 2 and store remainders in array.
- Divide the quotient by 2.
- Repeat step 2 until we get the quotient equal to zero.
- Equivalent binary number would be reverse of all remainders of step 1.

**B) Convert the fractional part of decimal to binary equivalent**

- Multiply the fractional decimal number by 2.
- Integral part of resultant decimal number will be first digit of fraction binary number.
- Repeat step 1 using only fractional part of decimal number and then step 2.

**C) Combine both integral and fractional part of binary number.**

**Illustration**

Let's take an example for n = 4.47 k = 3Step 1: Conversion of 4 to binary1. 4/2 : Remainder = 0 : Quotient = 2 2. 2/2 : Remainder = 0 : Quotient = 1 3. 1/2 : Remainder = 1 : Quotient = 0So equivalent binary of integral part of decimal is 100.Step 2: Conversion of .47 to binary1. 0.47 * 2 = 0.94, Integral part: 0 2. 0.94 * 2 = 1.88, Integral part: 1 3. 0.88 * 2 = 1.76, Integral part: 1So equivalent bianry of fractional part of decimal is .011Step 3: Combined the result of step 1 and 2.Final answer can be written as: 100 + .011 = 100.011

C++ Program to demonstrate above steps:

`// C++ program to convert fractional decimal ` `// to binary number ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to convert decimal to binary upto ` `// k-precision after decimal point ` `string decimalToBinary(` `double` `num, ` `int` `k_prec) ` `{ ` ` ` `string binary = ` `""` `; ` ` ` ` ` `// Fetch the integral part of decimal number ` ` ` `int` `Integral = num; ` ` ` ` ` `// Fetch the fractional part decimal number ` ` ` `double` `fractional = num - Integral; ` ` ` ` ` `// Conversion of integral part to ` ` ` `// binary equivalent ` ` ` `while` `(Integral) ` ` ` `{ ` ` ` `int` `rem = Integral % 2; ` ` ` ` ` `// Append 0 in binary ` ` ` `binary.push_back(rem +` `'0'` `); ` ` ` ` ` `Integral /= 2; ` ` ` `} ` ` ` ` ` `// Reverse string to get original binary ` ` ` `// equivalent ` ` ` `reverse(binary.begin(),binary.end()); ` ` ` ` ` `// Append point before conversion of ` ` ` `// fractional part ` ` ` `binary.push_back(` `'.'` `); ` ` ` ` ` `// Conversion of fractional part to ` ` ` `// binary equivalent ` ` ` `while` `(k_prec--) ` ` ` `{ ` ` ` `// Find next bit in fraction ` ` ` `fractional *= 2; ` ` ` `int` `fract_bit = fractional; ` ` ` ` ` `if` `(fract_bit == 1) ` ` ` `{ ` ` ` `fractional -= fract_bit; ` ` ` `binary.push_back(1 + ` `'0'` `); ` ` ` `} ` ` ` `else` ` ` `binary.push_back(0 + ` `'0'` `); ` ` ` `} ` ` ` ` ` `return` `binary; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `double` `n = 4.47; ` ` ` `int` `k = 3; ` ` ` `cout << decimalToBinary(n, k) << ` `"\n"` `; ` ` ` ` ` `n = 6.986 , k = 5; ` ` ` `cout << decimalToBinary(n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:100.011 110.11111

**Time complexity: **O(len(n))

**Auxiliary space: **O(len(n))

where len() is the total digits contain in number n.

Convert Binary fraction to Decimal

**Reference:**

http://cs.furman.edu/digitaldomain/more/ch6/dec_frac_to_bin.htm

http://www.cquestions.com/2011/07/c-program-for-fractional-decimal-to.html

This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Convert Binary fraction to Decimal
- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- Number of substrings with odd decimal value in a binary string
- Sum of decimal equivalent of all possible pairs of Binary representation of a Number
- Convert a binary number to octal
- Convert a binary number to hexadecimal number
- Convert all substrings of length 'k' from base 'b' to decimal
- Maximum rational number (or fraction) from an array
- Largest proper fraction with sum of numerator and denominator equal to a given number
- Program for Decimal to Binary Conversion
- Program for Binary To Decimal Conversion
- Decimal representation of given binary string is divisible by 5 or not
- Maximum decimal value path in a binary matrix
- Decimal representation of given binary string is divisible by 10 or not
- Decimal representation of given binary string is divisible by 20 or not