How to convert string to double
-
std::string to double
i tried stof but it didn't work
i kept getting errors
it didn't recognize it -
Hi karim24.
Did you try using "QString::toDouble()":http://qt-project.org/doc/qt-4.8/qstring.html#toDouble
Regards -
What does this have to do with Qt?
One of quite a few options:
@
#include <string>
#include <cstdlib>std::string test("0.6");
double value = ::atof(test.c_str());
@ -
It is recommended to use QString for Qt projects but if you insists on using std::string you can easily find solution in similar topics:
- "C++ string to double conversion":http://stackoverflow.com/questions/4754011/c-string-to-double-conversion
- "How can I convert string to double in C++?":http://stackoverflow.com/questions/392981/how-can-i-convert-string-to-double-in-c
- "std::string to float or double":http://stackoverflow.com/questions/1012571/stdstring-to-float-or-double
-
@ChrisW67
What does this have to do with Qt?
because i used "stof" in most of my code
when i was using visual c++ it worked fine
but when i used it in qtcreator there were errors
and now "atof" working just fine for me thanks -
This is a Qt forum, neither std::string nor stof() is part of Qt, and no other part of your question implicates Qt in any way. Hence my question.
You are probably getting errors, that you don't specify, because std::stof() is part of C++11 and your compiler, which you also did not specify, is defaulting to an earlier C++ standard. That's only a guess though.
-
The difference has to do with the compiler and libraries you use (VC vs MinGw), not with the fact that you use Qt or Qt Creator.