Java – long floating point output display letters
•
Java
I have the following code:
String curDir = "."; File fileObject = new File(curDir); File[] fileList = fileObject.listFiles(); float fileLengthMegabytes = (float)fileList[i].length() / 1000000;
Method filelist [i] Length() returns 311 bytes as long
The previous code produced the following output:
3.88E-4
How do I get the expected output of 0000311 in the filelengthmegabytes variable?
Solution
That's the scientific symbol
And you get 388 instead of 311 because you divide by 1000000 instead of 1048576 (1024 * 1024)
Edit: 311 it is not implemented even with 1048576, so you can get 370... So the error may be in your Calc);
As described here, you only need to convert your scientific symbols to decimal symbols through the formatter
DecimalFormat df = new DecimalFormat("#.########"); return df.format(fileLengthMegabytes);
Running example: http://ideone.com/2lkKv7
import java.util.*; import java.lang.*; import java.text.*; class Main { public static void main (String[] args) throws java.lang.Exception { DecimalFormat df = new DecimalFormat("#.##########"); float fileLengthMegabytes1 = (float) 388 / 1000000; float fileLengthMegabytes2 = (float) 388 / 1048576; System.out.println("MB1 in Scientific Notation: " + fileLengthMegabytes1); System.out.println("MB1 in Decimal Notation: " + df.format(fileLengthMegabytes1)); System.out.println("MB2 in Scientific Notation: " + fileLengthMegabytes2); System.out.println("MB2 in Decimal Notation: " + df.format(fileLengthMegabytes2)); } }
Output:
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码