Rechenfehler mit CSting...
-
hallo,
Visual Studio 2008 MFC nutze ich mit Win 7.
1.Problem: Warum bekomme ich 6 Stellen hinter dem Komma alles angezeigt?
2.Problem, ich bekomme Rechenfehler, wie:1.11 + 2.22 + 3.33 = 6.660003 <--- m_gpreis
float zsumme; float zwie = 0; // Schleife for(int ax = 0;ax < pos;ax++) { tempo = bestellung[ax][0]; // mein Textblock.Ursprünglich eine txt Datei zsumme = _tstof(tempo); zwie = zwie + zsumme; // zwischensumme + neue Summe } // Gesammtsumme von float in CString !!! tempo.Format(_T("%f"), zwie); m_gpreis = tempo; m_gpreis += " Euro"; UpdateData(FALSE); return 0;
-
Informiere dich mal genauer über Fließkomma-Datentypen wie float und double, da wirst du die Begründung finden.
Du verwendest auch noch float und das hat nur eine Genauigkeit von ca. 6 Stellen, double hat ca. 12.
Woher soll CString::Format wissen, wieviele Stellen nach dem Komma du haben willst. Also musst du es Format mitteilen (siehe printf-Doku)
anstatt: tempo.Format(_T("%f"), zwie);
schreibe: tempo.Format(_T("%.2f"), zwie);
für zwei Nachkommastellen
-
Softwaremaker schrieb:
Informiere dich mal genauer über Fließkomma-Datentypen wie float und double, da wirst du die Begründung finden.
Das hat was mit der Genauigkeit zutun, nicht mit der Darstellung.
Softwaremaker schrieb:
Du verwendest auch noch float und das hat nur eine Genauigkeit von ca. 6 Stellen, double hat ca. 12.
Woher soll CString::Format wissen, wieviele Stellen nach dem Komma du haben willst. Also musst du es Format mitteilen (siehe printf-Doku)
anstatt: tempo.Format(_T("%f"), zwie);
schreibe: tempo.Format(_T("%.2f"), zwie);
für zwei NachkommastellenGeht nicht, das gleiche in Blau
-
ups.
Der Code von Dir geht 1a.
sorry, habe den Punkt vergessen.
mega THX