views:

2282

answers:

9
+2  A: 

Yes, with a lexical cast. Use a stringstream and the << operator, or use Boost, they've already implemented it.

Your own version could look like:

template<typename to, typename from>to lexical_cast(from const &x) {
  std::stringstream os;
  to ret;

  os << x;
  os >> ret;

  return ret;  
}
DaClown
+6  A: 

Lexical cast is very nice.

#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>

using std::endl;
using std::cout;
using std::string;
using boost::lexical_cast;

int main() {
    string str = "0.6";
    double dub = lexical_cast<double>(str);
    cout << dub << endl;
}
sharth
+1 for not using "using namespace std;"
Johannes Schaub - litb
Thanks, it works.. But it's a question for me: why my code isn't working.
Ockonal
A: 

You can use boost lexical cast:

#include <boost/lexical_cast.hpp>

string v("0.6");
double dd = boost::lexical_cast<double>(v);
cout << dd << endl;

Note: boost::lexical_cast throws exception so you should be prepared to deal with it when you pass invalid value, try passing string("xxx")

stefanB
+1  A: 
haavee
Using a stringstream wouldn't require boost
jalf
Your method returns zero too. Linux.
Ockonal
+9  A: 
std::string num = "0.6";
double temp = ::atof(num.c_str());

Does it for me, it is a valid C++ syntax to convert a string to a double.

You can do it with the stringstream or boost::lexical_cast but those come with a performance penalty.


Ahaha you have a Qt project ...

QString winOpacity("0.6");
double temp = winOpacity.toDouble();

Extra note:
If the input data is a const char*, QByteArray::toDouble will be faster.

TimW
I don't see any reason why boost::lexical_cast would have a performance penalty (but a stringstream is certainly slower).
Zifre
boost::lexical_cast is streaming.
TimW
You can't generally say they come with a performance penalty, i think. Think about what happens when just before it you have a cin >> num;. The user would have to type very quickly (rly jon skeet like) to ever note the milliseconds lexical_cast is slower :) That said, i believe there are tasks where lexical_cast just sucks too much performance :)
Johannes Schaub - litb
... there is always a performance penalty, this penalty can be significant or not for the performance of the application. If the user can type very very very quickly it will be significant ;)
TimW
Thanks... I needn't in Boost. And i've forgot about Qt :D.
Ockonal
For this solution, what does the :: in front of atof() do? What does it need to be there?
ShaChris23
@ShaChris Because I want to make sure I use the atof function from the global namespace.
TimW
+1  A: 

You can use std::stringstream:

   #include <sstream>
   #include <string>
   template<typename T>
   T StringToNumber(const std::string& numberAsString)
   {
      T valor;

      std::stringstream stream(numberAsString);
      stream >> valor;
      if (stream.fail()) {
         std::runtime_error e(numberAsString);
         throw e;
      }
      return valor;
   }

Usage:

double number= StringToNumber<double>("0.6");
Edison Gustavo Muenz
+1  A: 

This answer is backing up litb in your comments. I have profound suspicions you are just not displaying the result properly.

I had the exact same thing happen to me once. I spent a whole day trying to figure out why I was getting a bad value into a 64-bit int, only to discover that printf was ignoring the second byte. You can't just pass a 64-bit value into printf like its an int.

T.E.D.
I'm not using printf to see results... And i use that value to set window opacity, and my window is full trasparent, so value is 0.
Ockonal
A: 

Rather than dragging Boost into the equation, you could keep your string (temporarily) as a char[] and use sprintf().

*But of course if you're using Boost anyway, it's really not too much of an issue.

Chris
+1  A: 
   double myAtof ( string &num){
      double tmp;
      sscanf ( num.c_str(), "%lf" , &tmp);
      return tmp;
   }
dpetek