Using atof with numbers that include a comma

Posted on 2004-11-07
Medium Priority
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
  • 2

Accepted Solution

mnashadka earned 600 total points
ID: 12517901
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

Author Comment

ID: 12518004
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


Expert Comment

ID: 12518106
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
Jaime Olivares earned 200 total points
ID: 12518292
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

840 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