📜  将二进制分数转换为十进制

📅  最后修改于: 2021-04-24 20:25:21             🧑  作者: Mango

给定一个二进制数n的字符串。将二进制小数n转换为等效的十进制数。

例子:

Input: n = 110.101
Output: 6.625

Input: n = 101.1101
Output: 5.8125


我们强烈建议您单击此处并进行实践,然后再继续解决方案。

以下是将二进制小数转换为十进制的步骤。

A)将二进制的整数部分转换为等效的十进制数

  1. 乘法各自单独从位小数点直到第一个数字的左侧由2 0,2 1,2 2,…分别。
  2. 添加所有来自步骤1的结果。
  3. 等效整数十进制数将是在步骤2中获得的结果。

B)将二进制的小数部分转换为等效的十进制数

  1. 由2 1除以从小数点直到末右侧每个数位,2 2,2 3,…分别。
  2. 添加所有来自步骤1的结果。
  3. 等效的小数十进制数将是在步骤2中获得的结果。

C)将十进制数的整数部分和小数部分相加。
插图

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



C++
// C++ program to demonstrate above steps of
// binary fractional to decimal conversion
#include
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


输出:

6.625
5.8125

时间复杂度: O(len(n))
辅助空间: O(len(n))

其中len是以n的二进制数包含的总位数。