Warum sind in C#0,025F und 0,025 0,025 m? [Duplikat]C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Warum sind in C#0,025F und 0,025 0,025 m? [Duplikat]

Post by Anonymous »

In C#habe ich diesen Code: < /p>
float initialResolutionFloat = 0.025f;
double initialResolutionDouble = 0.025;
decimal initialResolutionDecimal = 0.025m;

decimal resolutionFloat = (decimal)initialResolutionFloat;
decimal resolutionDouble = (decimal)initialResolutionDouble;
decimal resolutionDecimal = (decimal)initialResolutionDecimal;

bool floatEqual = resolutionFloat == resolutionDecimal;
bool doubleEqual = resolutionDouble == resolutionDecimal;

decimal expectedFloat = 0.0250000003725290298m;
decimal expectedDouble = 0.0250000000000000014m;

Console.WriteLine($"{resolutionFloat} {resolutionDouble} {resolutionDecimal} {floatEqual} {doubleEqual} {expectedFloat} {expectedDouble}");
< /code>
und es erzeugt dieses Ergebnis: < /p>
0.025 0.025 0.025 True True 0.0250000003725290298 0.0250000000000000014
< /code>
Und ich verstehe nicht warum. Ich hätte erwartet, dass die ersten beiden Werte den erwarteten ähnlich sind, aber was noch wichtiger ist, ich hätte erwartet, dass die beiden Gleichstellungsüberprüfungen scheitern. Microsoft Doc sagt: < /p>

Wenn Sie Float oder Double in Decimal konvertieren, wird der Quellwert in Dezimalverträte umgewandelt und nach dem 28. Dezimalplatz auf die nächste Zahl abgerundet.>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post