I’ve tried to look around for a solution to this, but I simply can’t.
I want to show the megabyte representation of a long that is calculated in bytes.
In Java, you got a method called length() that you can invoke on a file object. It will return the files size in bytes stored in a long.
Now, I want to that long and turn it into megabytes rather than bytes. A file I am trying with got the size 161000 bytes. If I am not mistaken, that translates to 0.161 megabytes. So I would do bytes / 100000 and I would expect to get 0.161. The problem is that I don’t.
If I go to my Windows Calculator, or any other calculator for that matter, the application can manage to show those 3 decimals. Why is it that I can’t do this?
I tried to store the result in a double, which just comes out as 0.0
EDIT:
An answers has been found.
Thanks to wrm:
long b = file.length();
double mb = (double) b / (1024 * 1024);
maybe a little code would clarify the problem but i think, you have some kind of implicit conversion going on.
something like this should work: