Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tonetheman/387454 to your computer and use it in GitHub Desktop.
Save tonetheman/387454 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
// convert string to double old school
double convert(char * s) {
int i=0;
double intPart = 0, fracPart = 0;
while(s[i]!='.') {
intPart = intPart*10 + (s[i]-'0');
i++;
}
i = strlen(s)-1;
while(s[i]!='.') {
fracPart = (fracPart + (s[i]-'0'))/10;
i--;
}
double conversion = intPart + fracPart;
return conversion;
}
int main() {
cout << convert("3.14159") << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment