Just find the decimal point ',' and convert it to '.' , then use atof from <cstdlib> :
#include <cstdlib> #include <cstdio> #include <string> double toDouble(std::string s){ // do not use a reference, since we're going to modify this string // If you do not care about ',' or '.' in your string use a // reference instead. size_t found = s.find(","); if(found != std::string::npos) s[found]='.'; // Change ',' to '.' return std::atof(s.c_str()); } int main(){ std::string aStr("0.012"); std::string bStr("0,012"); double aDbl = toDouble(aStr); double bDbl = toDouble(bStr); std::printf("%lf %lf\n",aDbl,bDbl); return 0; }
If you use the C string instead of std::string , use strchr from <cstring> to change the original string (remember to change it or work with a copy of the locale if you need the original version).
source share