0

I tried to cast a float value to an int, using this line of C# code:

int x = (int)(10.0f * 0.9f);

For some reasons, x now has the value 8, and I really don't see why. Can somebody help?

  • 1
    Floating point arithmetic is weird. It's probably resulting in something like 8.999999999999 and since you're casting to an int, the fractional component gets chopped. You could try `int x = (int)Math.Round(10.0f * 0.9f);` instead. – itsme86 Jun 24 '20 at 16:14
  • Does this answer your question? https://stackoverflow.com/questions/8911440/c-sharp-float-expression-strange-behavior-when-casting-the-result-float-to-int – mlibby Jun 24 '20 at 16:15
  • That worked, thanks! – ElSchneggo Jun 24 '20 at 16:19

0 Answers0