I have the following code:
String curDir = ".";
File fileObject = new File(curDir);
File[] fileList = fileObject.listFiles();
float fileLengthMegabytes = (float)fileList[i].length() / 1000000;
The method fileList[i].length() returns 311 bytes as the type Long.
The previous code results in the following output:
3.88E-4
How do I get my expected output of 0,000311 inside the fileLengthMegabytes variable?
That is Scientific Notation.
AND you are getting 388 instead of 311 because you are dividing by 1000000 instead of 1048576 (1024 * 1024)
EDIT: 311 is not achieved even with 1048576, that way you get 370… so the error is probably in your calc 😉
As described here , you just have to convert your Scientific Notation to a Decimal Notation through a Formatter.
Running Example: http://ideone.com/2lkKv7
Output: