help in convert String to float

This is a discussion on help in convert String to float within the C++ Programming forums, part of the General Programming Boards category; how to convert a string value to float or double? i use atoi function but giving me integer value sample ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    1

    help in convert String to float

    how to convert a string value to float or double?

    i use atoi function but giving me integer value

    sample code:

    Code:
    int main()
    {
    
    string a = "5.1234";
    float b;
    
    b = atoi(a.c_str());
    cout<<b;
    
    return 0;
    }
    sample output:
    5

    but i want to get is 5.1234
    got any solution?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    You can also use atof, which is the same as atoi but wprks with floating point values instead. BTW, generally you should use double instead of float unless you have a good reason.

  4. #4
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    atof is been subsumed by strtod but have been retained because they are used extensively in existing code. They are less reliable, but may be faster if the argument is known to be in a valid range.

    Use strtod instead because they offer more control over the conversion process, and because they are required not to produce unexpected results on overflow during conversion.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Those do the reverse.
    The first link uses C++ stringstreams. it is simple to reverse that process.
    Code:
    int StringToInt(string s)
    {
      stringstream myStream(s);
      int num;
      myStream >> num;
      if (!myStream)
      {
          cout << "ERROR: Not a number" << endl;
          return 0;
      }
      else
          return num;
    }

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Bench82
    The first link uses C++ stringstreams. it is simple to reverse that process.
    Code:
    int StringToInt(string s)
    {
      stringstream myStream(s);
      int num;
      myStream >> num;
      if (!myStream)
      {
          cout << "ERROR: Not a number" << endl;
          return 0;
      }
      else
          return num;
    }
    The OP wanted a float or double, but I agree with your point. Here's another version:

    [39.2] How do I convert a std::string to a number?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21