I ran into some trouble when converting bytes to gigabytes in my current project. Initially I did this:
long requiredDiskSpace = 5000000000000; // In bytes
int gb = (int)requiredDiskSpace / 1024 / 1024 / 1024;
This calculation becomes 0. (Correct should be 4 656). Then I switched to the decimal type, like this:
long requiredDiskSpace = 5000000000000; // In bytes
decimal gb = requiredDiskSpace / 1024 / 1024 / 1024;
int gbAsInt = (int)gb;
This calculation (correctly) makes gbAsInt 4 656.
Now, my question is simply; why? To me, the calculations look similar, as I’m not interested in any decimal numbers I don’t understand why I can’t just use int in the actual calculation.
The problem is
(int)requiredDiskSpace, the value5000000000000is way too big for an integer.