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

  • SEARCH
  • Home
  • 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 8383271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:07:27+00:00 2026-06-09T17:07:27+00:00

I have some rather mangled code that almost produces the tangible price/book from Yahoo

  • 0

I have some rather mangled code that almost produces the tangible price/book from Yahoo Finance for companies (a nice module called ystockquote gets the intangible price/book value already).

My problem is this:

For one of the variables in the calculation, shares outstanding I’m getting strings like 10.89B and 4.9M, where B and M stand respectively for billion and million. I’m having trouble converting them to numbers, here’s where I’m at:

shares=''.join(node.findAll(text=True)).strip().replace('M','000000').replace('B','000000000').replace('.','') for node in soup2.findAll('td')[110:112]

Which is pretty messy, but I think it would work if instead of

.replace('M','000000').replace('B','000000000').replace('.','') 

I was using a regular expression with variables. I guess the question is simply which regular expression and variables. Other suggestions are also good.

EDIT:

To be specific I’m hoping to have something that works for numbers with zero, one, or two decimals but these answers all look helpful.

  • 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-09T17:07:28+00:00Added an answer on June 9, 2026 at 5:07 pm
    >>> from decimal import Decimal
    >>> d = {
            'K': 3,
            'M': 6,
            'B': 9
    }
    >>> def text_to_num(text):
            if text[-1] in d:
                num, magnitude = text[:-1], text[-1]
                return Decimal(num) * 10 ** d[magnitude]
            else:
                return Decimal(text)
    
    >>> text_to_num('3.17B')
    Decimal('3170000000.00')
    >>> text_to_num('4M')
    Decimal('4000000')
    >>> text_to_num('4.1234567891234B')
    Decimal('4123456789.1234000000000')
    

    You can int() the result if you want too

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some rather odd behavior in my D program that I've narrowed down
Is there some way when sending this message to specify that I rather have
I have some rather long string containing just about anything that I want to
I have some code that has a dynamic-class system in C++ that has a
I have a rather large program that have some odd behaviour once in a
I have some code I need to write a test for that connects to
I'm about to have to rewrite some rather old code using SQL Server's BULK
I have some code in a reusable class that modifies some types. Here's a
I have one main thread that does some rather CPU intensive operation. The thread
I have some code that when called calls a webservice, queries a database and

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.