Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

madweekay

  • 3 years ago

Hi, does anybody know how to determine whether an input string (from raw_input) is a float? I know isdigit() works for int types, but what of float types? Or, is the only solution converting the string to a float(),and then assuming the program will crash for non-float input values?

  • This Question is Closed
  1. KonradZuse
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    depends on the language, but yes you can say Float.ParseFloat(string); in Java....

  2. KonradZuse
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    http://openstudy.com/study#/updates/50fba568e4b010aceb3312fb Someoen asked this yesterday in C it's Convert.

  3. madweekay
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I should clarify: This is for Python

  4. rsmith6559
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    You could do something like: try: x = float( raw_input( "> " ) ) except: #handle it not being a float

  5. madweekay
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks, this is pretty much what I'm going to do. I just wasn't sure if there was a built in function like isdigit()

  6. rsmith6559
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    There are always regular expressions. "\d*\.?\d*" off the top of my head.

  7. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy