Convert decimal fraction to binary number
Last Updated :
01 Mar, 2024
Given a fraction decimal number n and integer k, convert decimal number n into equivalent binary number up-to k precision after decimal point.
Examples:
Input: n = 2.47, k = 5
Output: 10.01111
Input: n = 6.986 k = 8
Output: 110.11111100
We strongly recommend that you click here and practice it, before moving on to the solution.
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 = 3
Step 1: Conversion of 4 to binary
1. 4/2 : Remainder = 0 : Quotient = 2
2. 2/2 : Remainder = 0 : Quotient = 1
3. 1/2 : Remainder = 1 : Quotient = 0
So equivalent binary of integral part of decimal is 100.
Step 2: Conversion of .47 to binary
1. 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: 1
So equivalent binary of fractional part of decimal is .011
Step 3: Combined the result of step 1 and 2.
Final answer can be written as:
100 + .011 = 100.011
Program to demonstrate above steps:
C++
#include<bits/stdc++.h>
using namespace std;
string decimalToBinary( double num, int k_prec)
{
string binary = "" ;
int Integral = num;
double fractional = num - Integral;
while (Integral)
{
int rem = Integral % 2;
binary.push_back(rem + '0' );
Integral /= 2;
}
reverse(binary.begin(),binary.end());
binary.push_back( '.' );
while (k_prec--)
{
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;
}
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;
}
|
Java
import java.util.*;
class GFG
{
static String decimalToBinary( double num, int k_prec)
{
String binary = "" ;
int Integral = ( int ) num;
double fractional = num - Integral;
while (Integral > 0 )
{
int rem = Integral % 2 ;
binary += (( char )(rem + '0' ));
Integral /= 2 ;
}
binary = reverse(binary);
binary += ( '.' );
while (k_prec-- > 0 )
{
fractional *= 2 ;
int fract_bit = ( int ) fractional;
if (fract_bit == 1 )
{
fractional -= fract_bit;
binary += ( char )( 1 + '0' );
}
else
{
binary += ( char )( 0 + '0' );
}
}
return binary;
}
static String reverse(String input)
{
char [] temparray = input.toCharArray();
int left, right = 0 ;
right = temparray.length - 1 ;
for (left = 0 ; left < right; left++, right--)
{
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.valueOf(temparray);
}
public static void main(String[] args)
{
double n = 4.47 ;
int k = 3 ;
System.out.println(decimalToBinary(n, k));
n = 6.986 ;
k = 5 ;
System.out.println(decimalToBinary(n, k));
}
}
|
Python3
def decimalToBinary(num, k_prec) :
binary = ""
Integral = int (num)
fractional = num - Integral
while (Integral) :
rem = Integral % 2
binary + = str (rem);
Integral / / = 2
binary = binary[ : : - 1 ]
binary + = '.'
while (k_prec) :
fractional * = 2
fract_bit = int (fractional)
if (fract_bit = = 1 ) :
fractional - = fract_bit
binary + = '1'
else :
binary + = '0'
k_prec - = 1
return binary
if __name__ = = "__main__" :
n = 4.47
k = 3
print (decimalToBinary(n, k))
n = 6.986
k = 5
print (decimalToBinary(n, k))
|
C#
using System;
class GFG
{
static String decimalToBinary( double num, int k_prec)
{
String binary = "" ;
int Integral = ( int ) num;
double fractional = num - Integral;
while (Integral > 0)
{
int rem = Integral % 2;
binary += (( char )(rem + '0' ));
Integral /= 2;
}
binary = reverse(binary);
binary += ( '.' );
while (k_prec-- > 0)
{
fractional *= 2;
int fract_bit = ( int ) fractional;
if (fract_bit == 1)
{
fractional -= fract_bit;
binary += ( char )(1 + '0' );
}
else
{
binary += ( char )(0 + '0' );
}
}
return binary;
}
static String reverse(String input)
{
char [] temparray = input.ToCharArray();
int left, right = 0;
right = temparray.Length - 1;
for (left = 0; left < right; left++, right--)
{
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.Join( "" ,temparray);
}
public static void Main(String[] args)
{
double n = 4.47;
int k = 3;
Console.WriteLine(decimalToBinary(n, k));
n = 6.986;
k = 5;
Console.WriteLine(decimalToBinary(n, k));
}
}
|
PHP
<?php
function decimalToBinary( $num , $k_prec )
{
$binary = "" ;
$Integral = (int)( $num );
$fractional = $num - $Integral ;
while ( $Integral )
{
$rem = $Integral % 2;
$binary .= chr ( $rem + 48 );
$Integral = (int)( $Integral /2);
}
$binary = strrev ( $binary );
$binary .= '.' ;
while ( $k_prec --)
{
$fractional *= 2;
$fract_bit = (int) $fractional ;
if ( $fract_bit == 1)
{
$fractional -= $fract_bit ;
$binary .= chr (1 + 48 );
}
else
$binary .= chr (0 + 48 );
}
return $binary ;
}
$n = 4.47;
$k = 3;
echo decimalToBinary( $n , $k ). "\n" ;
$n = 6.986;
$k = 5;
echo decimalToBinary( $n , $k );
?>
|
Javascript
<script>
function decimalToBinary(num, k_prec)
{
let binary = "" ;
let Integral = parseInt(num, 10);
let fractional = num - Integral;
while (Integral > 0)
{
let rem = Integral % 2;
binary +=
(String.fromCharCode(rem + '0' .charCodeAt()));
Integral = parseInt(Integral / 2, 10);
}
binary = reverse(binary);
binary += ( '.' );
while (k_prec-- > 0)
{
fractional *= 2;
let fract_bit = parseInt(fractional, 10);
if (fract_bit == 1)
{
fractional -= fract_bit;
binary +=
String.fromCharCode(1 + '0' .charCodeAt());
}
else
{
binary +=
String.fromCharCode(0 + '0' .charCodeAt());
}
}
return binary;
}
function reverse(input)
{
let temparray = input.split( '' );
let left, right = 0;
right = temparray.length - 1;
for (left = 0; left < right; left++, right--)
{
let temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return temparray.join( "" );
}
let n = 4.47;
let k = 3;
document.write(decimalToBinary(n, k) + "</br>" );
n = 6.986;
k = 5;
document.write(decimalToBinary(n, k));
</script>
|
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...