Take the 2-minute tour ×

I have this:

double myDecimal = static_cast<double>(atoi(arg_vec[1]));
cout << myDecimal << endl;

But why when I pass the argument like this:

./MyCode 0.003

It prints 0 instead of 0.003.

share|improve this question

4 Answers 4

up vote 7 down vote accepted

atoi() converts to integer. You want atof().

Or you could use strtod().

share|improve this answer

atoi() converts to an integer, you want atof(), which converts to a double

share|improve this answer

Since you're using C++, you can also use stringstreams:

istringstream ss(arg_vec[1]);
double d;
ss >> d;
share|improve this answer
double d = boost::lexical_cast<double>("0.003");
share|improve this answer

Your Answer


