Can't get accurate file size
Question
This is how I try to get the file size in MB:
FileInfo file_size = new FileInfo(list[i]);
double friendly_file_size = (file_size.Length / 1048576);
MessageBox.Show(friendly_file_size.ToString());
The problem is it does not show anything after comma.. when the file size is 3.15, it says 3. When it is 0.5, is says 0, what am I doing wrong?
Solution
That's an integer division. Turn it into a floating point division by making at least one of the operands a floating pointer number:
double friendly_file_size = file_size.Length / 1048576.0;
or:
double friendly_file_size = (double)file_size.Length / 1048576;
OTHER TIPS
Since your denominator is an integer, I suspect a cast is forcing integer division. The solution is simple. Just cast the denominator to a double. Like this:
double friendly_file_size = file_size.Length / (double)1048576;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow