Rundungsfehler?
-
Hi Leute!

Folgender Code aus main():float eurobetrag = 123.21; int centbetrag = 0; centbetrag = int(eurobetrag *100); cout << centbetrag;Ich möchte Euro in Cent umwandeln. Bei diesem Beispiel macht der GNU++ aber 12320 cent draus.
Also ist 1 cent verschwunden. Ist das ne Rundungssache? Wie kann ich das fixen?
PS: Ich weiß, man sollte nicht mit float rechnen.Mfg FBRider
-
#include <stdio.h>
#define DIRTY_CONVERSION int main()DIRTY_CONVERSION
{
char buf[100] = {0};
int cent = 0;
float eur = 123.21;_gcvt( eur * 100, 5, buf );
cent = atoi(buf);printf("%d", cent );
return 0;
}
-
FBRider schrieb:
Wie kann ich das fixen?
nicht mit float rechnen.
PS: Ich weiß, man sollte nicht mit float rechnen.
oh.