Convert Binary fraction to Decimal (original) (raw)

Last Updated : 13 Sep, 2023

Given an string of binary number n. Convert binary fractional n into it's decimal equivalent.

Examples:

Input: n = 110.101 Output: 6.625

Input: n = 101.1101 Output: 5.8125

We strongly recommend that you click here and practice it, before moving on to the solution.

Following are the steps of converting binary fractional to decimal.

A) Convert the integral part of binary to decimal equivalent

  1. Multiply each digit separately from left side of radix point till the first digit by 20, 21, 22,... respectively.
  2. Add all the result coming from step 1.
  3. Equivalent integral decimal number would be the result obtained in step 2.

B) Convert the fractional part of binary to decimal equivalent

  1. Divide each digit from right side of radix point till the end by 21, 22, 23, ... respectively.
  2. Add all the result coming from step 1.
  3. Equivalent fractional decimal number would be the result obtained in step 2.

C) Add both integral and fractional part of decimal number.
Illustration

Let's take an example for n = 110.101

Step 1: Conversion of 110 to decimal => 1102 = (122) + (121) + (0*20) => 1102 = 4 + 2 + 0 => 1102 = 6 So equivalent decimal of binary integral is 6.

Step 2: Conversion of .101 to decimal => 0.1012 = (11/2) + (01/22) + (11/23) => 0.1012 = 10.5 + 00.25 + 10.125 => 0.1012 = 0.625 So equivalent decimal of binary fractional is 0.625

Step 3: Add result of step 1 and 2. => 6 + 0.625 = 6.625

Implementation:

C++ `

// C++ program to demonstrate above steps of // binary fractional to decimal conversion #include<bits/stdc++.h> using namespace std;

// Function to convert binary fractional to // decimal double binaryToDecimal(string binary, int len) { // Fetch the radix point size_t point = binary.find('.');

// Update point if not found
if (point == string::npos)
    point = len;

double intDecimal = 0, fracDecimal = 0, twos = 1;

// Convert integral part of binary to decimal
// equivalent
for (int i = point-1; i>=0; --i)
{
    // Subtract '0' to convert character
    // into integer
    intDecimal += (binary[i] - '0') * twos;
    twos *= 2;
}

// Convert fractional part of binary to
// decimal equivalent
twos = 2;
for (int i = point+1; i < len; ++i)
{
    fracDecimal += (binary[i] - '0') / twos;
    twos *= 2.0;
}

// Add both integral and fractional part
return intDecimal + fracDecimal;

}

// Driver code int main() { string n = "110.101"; cout << binaryToDecimal(n, n.length()) << "\n";

n = "101.1101";
cout << binaryToDecimal(n, n.length());

return 0;

}

Java

// Java program to demonstrate above steps of // binary fractional to decimal conversion import java.io.*;

class GFG{

// Function to convert binary fractional to // decimal static double binaryToDecimal(String binary, int len) {

// Fetch the radix point
int point = binary.indexOf('.');

// Update point if not found
if (point == -1)
    point = len;

double intDecimal = 0, 
       fracDecimal = 0,
       twos = 1;

// Convert integral part of binary to decimal
// equivalent
for(int i = point - 1; i >= 0; i--)
{
    intDecimal += (binary.charAt(i) - '0') * twos;
    twos *= 2;
}

// Convert fractional part of binary to
// decimal equivalent
twos = 2;
for(int i = point + 1; i < len; i++)
{
    fracDecimal += (binary.charAt(i) - '0') / twos;
    twos *= 2.0;
}

// Add both integral and fractional part
return intDecimal + fracDecimal;

}

// Driver Code public static void main(String[] args) { String n = "110.101"; System.out.println( binaryToDecimal(n, n.length()));

n = "101.1101";
System.out.println(
    binaryToDecimal(n, n.length()));

} }

// This code is contributed by dheeraj_2801

Python3

Python3 program to demonstrate above steps

of binary fractional to decimal conversion

Function to convert binary fractional

to decimal

def binaryToDecimal(binary, length) :

# Fetch the radix point 
point = binary.find('.')

# Update point if not found 
if (point == -1) :
    point = length 

intDecimal = 0
fracDecimal = 0
twos = 1

# Convert integral part of binary 
# to decimal equivalent 
for i in range(point-1, -1, -1) : 
    
    # Subtract '0' to convert 
    # character into integer 
    intDecimal += ((ord(binary[i]) - 
                    ord('0')) * twos) 
    twos *= 2

# Convert fractional part of binary 
# to decimal equivalent 
twos = 2

for i in range(point + 1, length):
    
    fracDecimal += ((ord(binary[i]) -
                     ord('0')) / twos); 
    twos *= 2.0

# Add both integral and fractional part 
ans = intDecimal + fracDecimal

return ans

Driver code :

if name == "main" : n = "110.101" print(binaryToDecimal(n, len(n)))

n = "101.1101"
print(binaryToDecimal(n, len(n)))

This code is contributed

by aishwarya.27

C#

// C# program to demonstrate above steps of // binary fractional to decimal conversion using System;

class GFG{

// Function to convert binary fractional to // decimal static double binaryToDecimal(string binary, int len) {

// Fetch the radix point 
int point = binary.IndexOf('.'); 

// Update point if not found 
if (point == -1) 
    point = len; 

double intDecimal = 0,  
       fracDecimal = 0, 
       twos = 1; 

// Convert integral part of binary to decimal 
// equivalent 
for(int i = point - 1; i >= 0; i--) 
{ 
    intDecimal += (binary[i] - '0') * twos; 
    twos *= 2; 
} 

// Convert fractional part of binary to 
// decimal equivalent 
twos = 2; 
for(int i = point + 1; i < len; i++) 
{ 
    fracDecimal += (binary[i] - '0') / twos; 
    twos *= 2.0; 
} 

// Add both integral and fractional part 
return intDecimal + fracDecimal; 

}

// Driver Code public static void Main(string[] args) { string n = "110.101"; Console.Write( binaryToDecimal(n, n.Length) + "\n");

n = "101.1101"; 
Console.Write( 
    binaryToDecimal(n, n.Length)); 

} }

// This code is contributed by rutvik_56

JavaScript

`

Time complexity: O(len(n))
Auxiliary space: O(len(n))

Where len is the total digits contain in binary number of n.

See this: Convert decimal fraction to binary number.