views:

190

answers:

4

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.

+3  A: 

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

anon
+7  A: 

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

Or you could use strtod().

Mitch Wheat
+2  A: 

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

istringstream ss(arg_vec[1]);
double d;
ss >> d;
greyfade
+1  A: 
double d = boost::lexical_cast<double>("0.003");
MSalters