Using atof with numbers that include a comma

Posted on 2004-11-07
Last Modified: 2012-08-13

I have this line of code:

float f= (float) atof ("1,024");

f turns into 1, not 1024.

why is the CRT treating comma as a period ? and if it is a period, where is the 1.024 value ?

anyway, I would like to retrieve a numeric value of 1024, how can that be done ?


is there a better, more modern C++ way to convert for alpha to numeric and vice versa ?


Question by:moshem
    LVL 8

    Accepted Solution

    C++ (or, in your case, C) does not have any standard method of dealing with numbers with commas.  atof parses the number up until the first invalid character (a non-numeric character that's not either a period or an 'E'), and stops there.  The best thing to do is to remove the commas before converting it.

    A more C++ way to do it (again, without commas) would be to use the stringstream.  Something like:
    double x;
    istringstream number_stream("1024"); // Construct with the character string or std::string
    number_stream >> x; // Extract from the stream
    LVL 1

    Author Comment

    how can I use

    istringstream number_stream("1024");

    a second time ?

    is there a number_stream = 2048;

    why is the C++ version better ? except for more order

    LVL 8

    Expert Comment

    Probably the easiest thing to do there is move it into a function, but if you're always working with char arrays, then using atof or atoi or whatever would work just fine.  The stringstream classes are just better because you can use them to convert any class that has a stream insertion operator into any other class that has a stream extraction operator.  For instance, you could write a template function that does this for you, like:

    template<typename new_type, typename type>
    new_type cast(const type &argument)
      std::stringstream stream;
      new_type result; // Variable to hold the result
      stream << argument;
      stream >> result;
      return result;

    Then to cast back and forth between int and char array or std::string, something like:
      double x = cast<double>("1024"); // Cast it to a double
      cout << x << endl;
      std::string s = cast<std::string>(x); // Cast it back to a string
      cout << s << endl;

    You could also use it on your own classes, like:
    class A
      A(const int &value) : x(value) {} // Initialize it to the sent-in value

      friend std::ostream &operator<<(std::ostream &stream, const A &a);

      int x;

    std::ostream &operator<<(std::ostream &stream, const A &a)
      stream << a.x;
      return stream;

    class B
      void Print() { cout << "B::Print(): y = " << y << endl; }

      friend std::istream &operator>>(std::istream &stream, B &b);

      std::string y;

    std::istream &operator>>(std::istream &stream, B &b)
      stream >> b.y;
      return stream;

    Then cast back and forth like:
      A a(210);
      B b = cast<B>(a); // Cast A to B
      b.Print(); // Show the result

    That being said, the C version is much more efficient.  This is just way more flexible.
    LVL 55

    Assisted Solution

    by:Jaime Olivares
    To remove commas in C++ way, you can use this:

    string amount = "1,234,567";
    int br;
    while((br = amount .find(',')) != string::npos) amount .erase(br, 1);

    Now you can make conversion:

    float f= (float) atof(amount .c_str());


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now