Thread: Convert string to float

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    Convert string to float

    If not use atof, how can I complete the following function which can convert string to float?? Thanks..

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <math.h>
    
    float strToFloat(const char s[]) {
    
      /* Add your code here */
    
    }
    
    int main() {
      printf("%f\n", strToFloat("1"));
      printf("%f\n", strToFloat("   1.23"));
      printf("%f\n", strToFloat("-1.23"));
      printf("%f\n", strToFloat("1.23456"));
      printf("%f\n", strToFloat("0.00000001"));
      printf("%f\n", strToFloat("0.0.1"));
      printf("%f\n", strToFloat("1E2"));
      printf("%f\n", strToFloat("0.0.1"));
      printf("%f\n", strToFloat("1.23456E2"));
      printf("%f\n", strToFloat("1.23456e3"));
      printf("%f\n", strToFloat("2E"));
    }

  2. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    Probably the easyest way to do this is to use sscanf().
    Code:
    float fl;
    
    sscanf(s, "%f", &fl);
    
    return fl;
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2
    Thank you.
    But I can't use sscanf for the question. Is there any other function that I can use?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    You could just write necessary logic to convert the strings to floats yourself.

    If you haven't done anything of this sort before, try your hand at making a basic ASCII to Integer function first. Once you can do this, you should be a lot closer to turning a string into a float.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    strtod() is part of the standard library, otherwise you'll have to sscanf it yourself

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Yep, yep - you could look at strtod. I generally use it for floating point input, and strtol for integer input. Have a look at this for an idea of how to implement your code. Of course the linked page deals with strtol, but after reading your documentation for both strtol and strtod, it is fairly trivial to use strtod in place of strtol for your needs.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Do this using side-by-side development, i.e. check that your code gives the same answer as sscanf.
    Other than that, just approach this one character at a time. Look at the first character, and adjust the value of your float or other member variables accordingly, then move on to the next character. You'll need something to track the sign of the number, and whether you've seen the sign yet, and something to track whether you've seen the decimal point yet, etc.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert string to float?
    By tesla in forum C++ Programming
    Replies: 7
    Last Post: 09-22-2009, 05:39 AM
  2. Model Rocket Altitude predictor...
    By kalor_alros in forum C++ Programming
    Replies: 11
    Last Post: 09-04-2009, 12:27 AM
  3. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  4. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  5. help me
    By warthog89 in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 08:17 AM