Using atof with numbers that include a comma


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 ?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
moshemAuthor Commented:
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

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.
Jaime OlivaresSoftware ArchitectCommented:
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());

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.