String to double -> mit Exponenten!
-
hallo
ich habe folgendes Problem:ich habe einen String -> "1.1235464+e015" diesen String möchte ich gern in ein double konvertieren. Ich habe es bis jetzt so gelöst :
double stringZuZahl(string s) { stringstream sst; double temp; sst << s; sst >> temp; return temp; }da ist aber das problem, dass diese Funktion ab dem "+" abschneidet. also kommt z.b. als double zahl folgendes herraus: 1.1235464
wenn ich also damit nun weiterrechnen will kommt natürlcih nicht das richtige Ergebnis heraus -> da er nicht mit der richtigen Zahl rechnet.
Meine frage ist wie ich dieses Problem lösen kann -> ich habe auch die Funktion atof() ausprobiert -> selbes problem.
MFg
-
indem du den String richtig formatierst: "1.23464**e+**15" Das was du da stehen hast ist ein double (1.23464) plus (+) irgendein Wust, den niemand so richtig entschluesseln kann (was soll "e015" bedeuten?)
-
ach na dann brauch ich mich net wundern
-> sorry aber trozdem THX ^^
-
Und kopier nicht so oft unnötig!
double string_to_number(std::string const& string) /* const-Ref. => Kopieren sparen! */ { std::istringstream ss(string); // direkt in Konstruktor packen. operator<< aufruf gespart! double tmp; ss >> tmp; return temp; }
Jetzt kanst du da auch ein Template draus amchen, damit es auch bei Integer usw. geht!template<typename destination_type> destination_type string_to_number(std::string const& string) { std::istringstream ss(string); destination_type tmp; if (!(ss >> tmp)) throw std::bad_cast("no number"); return temp; }so bekommst du sogar ne Exception um die Ohren, wenn du eine "nicht Zahl" eingibst
