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);
Long variables can hold numbers from -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807. Operations with Long are slightly slower than with Integer . If you need even larger values, you can use the Decimal Data Type.
long is just a big integer, it has nothing past the decimal point.
Converting a string to a decimal value or decimal equivalent can be done using the Decimal. TryParse() method. It converts the string representation of a number to its decimal equivalent.
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:
double result = (double)myLongVal / 1024.0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With