Convert String to double/float
In some cases, it is useful to convert a number inside a string to a double or float. This can easily be done with the function atof, as the following code shows:
#include <iostream.h>
#include <string>

using namespace std;

int main () {
string somestr = "4.4982";
double number = atof(somestr.c_str());
cout << number+1; // output: 5.4982

If you want to have a char array as the input of atof, don't forget to terminate it with the char '\0'!
admin / Dec 05, 2008
2004 - 2021 /