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
- Multiply each digit separately from left side of radix point till the first digit by 20, 21, 22,… respectively.
- Add all the result coming from step 1.
- Equivalent integral decimal number would be the result obtained in step 2.
B) Convert the fractional part of binary to decimal equivalent
- Divide each digit from right side of radix point till the end by 21, 22, 23, … respectively.
- Add all the result coming from step 1.
- 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 = (1*22) + (1*21) + (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 = (1*1/2) + (0*1/22) + (1*1/23)
=> 0.1012 = 1*0.5 + 0*0.25 + 1*0.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++
#include<bits/stdc++.h>
using namespace std;
double binaryToDecimal(string binary, int len)
{
size_t point = binary.find( '.' );
if (point == string::npos)
point = len;
double intDecimal = 0, fracDecimal = 0, twos = 1;
for ( int i = point-1; i>=0; --i)
{
intDecimal += (binary[i] - '0' ) * twos;
twos *= 2;
}
twos = 2;
for ( int i = point+1; i < len; ++i)
{
fracDecimal += (binary[i] - '0' ) / twos;
twos *= 2.0;
}
return intDecimal + fracDecimal;
}
int main()
{
string n = "110.101" ;
cout << binaryToDecimal(n, n.length()) << "\n" ;
n = "101.1101" ;
cout << binaryToDecimal(n, n.length());
return 0;
}
|
Java
import java.io.*;
class GFG{
static double binaryToDecimal(String binary,
int len)
{
int point = binary.indexOf( '.' );
if (point == - 1 )
point = len;
double intDecimal = 0 ,
fracDecimal = 0 ,
twos = 1 ;
for ( int i = point - 1 ; i >= 0 ; i--)
{
intDecimal += (binary.charAt(i) - '0' ) * twos;
twos *= 2 ;
}
twos = 2 ;
for ( int i = point + 1 ; i < len; i++)
{
fracDecimal += (binary.charAt(i) - '0' ) / twos;
twos *= 2.0 ;
}
return intDecimal + fracDecimal;
}
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()));
}
}
|
Python3
def binaryToDecimal(binary, length) :
point = binary.find( '.' )
if (point = = - 1 ) :
point = length
intDecimal = 0
fracDecimal = 0
twos = 1
for i in range (point - 1 , - 1 , - 1 ) :
intDecimal + = (( ord (binary[i]) -
ord ( '0' )) * twos)
twos * = 2
twos = 2
for i in range (point + 1 , length):
fracDecimal + = (( ord (binary[i]) -
ord ( '0' )) / twos);
twos * = 2.0
ans = intDecimal + fracDecimal
return ans
if __name__ = = "__main__" :
n = "110.101"
print (binaryToDecimal(n, len (n)))
n = "101.1101"
print (binaryToDecimal(n, len (n)))
|
C#
using System;
class GFG{
static double binaryToDecimal( string binary,
int len)
{
int point = binary.IndexOf( '.' );
if (point == -1)
point = len;
double intDecimal = 0,
fracDecimal = 0,
twos = 1;
for ( int i = point - 1; i >= 0; i--)
{
intDecimal += (binary[i] - '0' ) * twos;
twos *= 2;
}
twos = 2;
for ( int i = point + 1; i < len; i++)
{
fracDecimal += (binary[i] - '0' ) / twos;
twos *= 2.0;
}
return intDecimal + fracDecimal;
}
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));
}
}
|
Javascript
<script>
function binaryToDecimal(binary, len) {
var point = binary.indexOf( "." );
if (point === -1) point = len;
var intDecimal = 0,
fracDecimal = 0,
twos = 1;
for ( var i = point - 1; i >= 0; i--) {
intDecimal += (binary[i] - "0" ) * twos;
twos *= 2;
}
twos = 2;
for ( var i = point + 1; i < len; i++) {
fracDecimal += (binary[i] - "0" ) / twos;
twos *= 2.0;
}
return intDecimal + fracDecimal;
}
var n = "110.101" ;
document.write(binaryToDecimal(n, n.length) + "<br>" );
n = "101.1101" ;
document.write(binaryToDecimal(n, n.length));
</script>
|
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.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
13 Sep, 2023
Like Article
Save Article