C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem beim konvertieren einse strings in einen float, bzw.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German)
View previous topic :: View next topic  
Author Message
Alexander
Guest





PostPosted: Mon Nov 10, 2003 4:11 pm    Post subject: Problem beim konvertieren einse strings in einen float, bzw. Reply with quote



hallo newsgroup,

hab ein kleines problem:

ich lese aus einem file eine zeile ein und zerhake diesen.
danach wandle ich die teilstrings in c_strings um, um danach float aus
diesen zu machen.

also:
string teil1;
float tmp_1;

*---------schnipp----------*

tmp_1 = atof(teil1.c_str());

cout << tmp_1

problem: teil1 ist z.b. 1234.0
tmp_1 ist bei cout 1234
also ohne null.

wie bekomme ich wieder die nachkommanull?

oder geht das einfacher?

danke für eure hilfe

gruß

Alexander

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Lothar Dierkes
Guest





PostPosted: Tue Nov 11, 2003 8:33 am    Post subject: Re: Problem beim konvertieren einse strings in einen float, Reply with quote



Alexander wrote:
Quote:

problem: teil1 ist z.b. 1234.0
tmp_1 ist bei cout 1234
also ohne null.

wie bekomme ich wieder die nachkommanull?

oder geht das einfacher?

danke für eure hilfe

#include<iostream>

using namespace std;

int main(void){
float f = 234.0;
cout.setf(ios_base::fixed);
cout.precision(6);
cout << f << endl;
return 0;
}
Quote:

gruß

Alexander

Lothar

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Alexander
Guest





PostPosted: Tue Nov 11, 2003 9:35 am    Post subject: Re: Problem beim konvertieren einse strings in einen float, Reply with quote



danke für deine hilfe.

Quote:
#include
using namespace std;

int main(void){
float f = 234.0;
cout.setf(ios_base::fixed);
cout.precision(6);
cout << f << endl;
return 0;
}

funzt nur nicht ganz, da durch fixed meine nachkommastellen willkürlich
aufgefüllt werden.
aber mit ios_base::showpoint hatte es dann doch funktioniert!

danke nochmals für die hilfe

gruß
alexander

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Werner Salomon
Guest





PostPosted: Tue Nov 11, 2003 12:12 pm    Post subject: Re: Problem beim konvertieren einse strings in einen float, Reply with quote

"Alexander" <camek (AT) in (DOT) tum.de> wrote

Hallo Alexander,

Quote:
ich lese aus einem file eine zeile ein und zerhake diesen.
danach wandle ich die teilstrings in c_strings um, um danach float aus
diesen zu machen.
im allgemeinen ist es besser den 'float' unmittelbar aus der Datei zu

lesen - ich unterstelle mal, dass die float-Werte im ASCII-Format
vorliegen.

Quote:
also:
string teil1;
float tmp_1;

*---------schnipp----------*

tmp_1 = atof(teil1.c_str());
... besser ist hier direkt

file >> tmp_1;

Quote:
cout << tmp_1

problem: teil1 ist z.b. 1234.0
tmp_1 ist bei cout 1234
also ohne null.

wie bekomme ich wieder die nachkommanull?

kein Problem! rein mathematisch ist 1234.0 natürlich identisch mit
1234. Also ist der Wert von 'tmp_1' in beiden Fällen gleich!
Ersteres ist lediglich eine andere Form der lesbare Ausgabe dieses
Float-Wertes auf 'cout'.

Eine Ausgabe mit einer Nachkomma-Stelle bekommst Du mit:

cout << fixed << setprecision(1) << tmp_1 << endl;

... natürlich brauchst Du das .. fixed << setprecision(1).. nur einmal
angeben und nicht vor jedem float, welches Du ausgeben möchtest. Schau
Dir die Hilfe in Deiner Entwicklungsumgebung zu 'fixed' und
'setprecision' an.

Gruß
Werner

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Alexander
Guest





PostPosted: Wed Nov 12, 2003 7:49 am    Post subject: Re: Problem beim konvertieren einse strings in einen float, Reply with quote

Quote:
funzt nur nicht ganz, da durch fixed meine nachkommastellen willkürlich
aufgefüllt werden.
aber mit ios_base::showpoint hatte es dann doch funktioniert!

Bei meiner Variante wird f immer mit 6 Nachkommastellen ausgegeben.
Bei deiner Variante wird f immer 2 Nachkommastellen ausgegeben. Ich
verstehe nicht ganz wo da die Willkür herscht, und würde gern aufgeklärt
werden Wink .

es bezog sich nicht auf die variante!
irgendwie habe ich bei deiner variante in der ausgabe willkürlich mit
zufälligen zahlen aufgefüllte nachkommastellen.
eigentlich müssten es nur Nullen sein.
das war mir ein bischen sonderbar!

gruß
alexander

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Lothar Dierkes
Guest





PostPosted: Wed Nov 12, 2003 1:12 pm    Post subject: Re: Problem beim konvertieren einse strings in einen float, Reply with quote

Alexander wrote:
Quote:
...
cout.setf(ios_base::fixed);
cout.precision(6);
cout << f << endl;
...

funzt nur nicht ganz, da durch fixed meine nachkommastellen willkürlich
aufgefüllt werden.
aber mit ios_base::showpoint hatte es dann doch funktioniert!

Bei meiner Variante wird f immer mit 6 Nachkommastellen ausgegeben.
Bei deiner Variante wird f immer 2 Nachkommastellen ausgegeben. Ich
verstehe nicht ganz wo da die Willkür herscht, und würde gern aufgeklärt
werden Wink .

Quote:
gruß
alexander

Lothar
--
http://gnu.kldp.org/cb/hacker-howto/the_girls_guide_to_geek_guys.html

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.