Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8927373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:12:41+00:00 2026-06-15T08:12:41+00:00

Let us suppose that we have a string representing a binary fraction such as:

  • 0

Let us suppose that we have a string representing a binary fraction such as:

".1"

As a decimal number this is 0.5. Is there a standard way in Python to go from such strings to a number type (whether it is binary or decimal is not strictly important).

For an integer, the solution is straightforward:

int("101", 2)
>>>5

int() takes an optional second argument to provide the base, but float() does not.

I am looking for something functionally equivalent (I think) to this:

def frac_bin_str_to_float(num):
    """Assuming num to be a string representing
    the fractional part of a binary number with
    no integer part, return num as a float."""
    result = 0
    ex = 2.0
    for c in num:
        if c == '1':
            result += 1/ex 
        ex *= 2
    return result

I think that does what I want, although I may well have missed some edge cases.

Is there a built-in or standard method of doing this in Python?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:12:42+00:00Added an answer on June 15, 2026 at 8:12 am

    The following is a shorter way to express the same algorithm:

    def parse_bin(s):
        return int(s[1:], 2) / 2.**(len(s) - 1)
    

    It assumes that the string starts with the dot. If you want something more general, the following will handle both the integer and the fractional parts:

    def parse_bin(s):
        t = s.split('.')
        return int(t[0], 2) + int(t[1], 2) / 2.**len(t[1])
    

    For example:

    In [56]: parse_bin('10.11')
    Out[56]: 2.75
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's suppose I have a string that contains 7.2769482308e+01 (this number came from 3rd
Suppose that we have a System.Decimal number. For illustration, let's take one whose ToString()
Let's suppose I have a Winforms app with a Dictionary that looks like this:
Suppose I have this class: type Pet (name:string) as this = let mutable age
Let's suppose I have a string like this $str = this is my string;
Let's suppose that I have 2 processes in Erlang, and each process has a
First suppose that I have an abstract class, let's call it AbstractClass. Suppose public
Suppose that I have a request handler that accepts an argument: key And let
Let's suppose I've to test this Java ClassA that depends on the difficult to
I suppose that this is an interesting code example. We have a class --

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.