1

I am running into an issue with a large float in C# code that I need to convert to an int32:

int test = (int)199999900f;

Test ends up being set to 199999904.

Can anyone explain what is going on here an why I gain 4 out of the conversion in that one line of code?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

2

Float have only a limited amount of storage (implementation dependent, probably 32 bit), so you exceeded that. Basically, the 9th digit cannot be stored anymore in an float. You should use Double instead of float, which gives you more space.

If your number is a constant, like shown in the example, you shouldn't use a ny float at all, but just leave it as an int.

Aganju
  • 6,295
  • 1
  • 12
  • 23