Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long float-number output shows letters

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?

like image 408
Birdman Avatar asked Nov 13 '12 15:11

Birdman


People also ask

Is 0.0 A float number?

A floating point number, is a positive or negative whole number with a decimal point. For example, 5.5, 0.25, and -103.342 are all floating point numbers, while 91, and 0 are not.

How is a float number represented in float datatype?

Floating-point variables are represented by a mantissa, which contains the value of the number, and an exponent, which contains the order of magnitude of the number. The following table shows the number of bits allocated to the mantissa and the exponent for each floating-point type.

What is long float number?

A long floating-point number (type long-float) is of the representation of the largest fixed precision provided by an implementation. Intermediate between short and long formats are two others, arbitrarily called single and double (types single-float and double-float).


2 Answers

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.

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:

MB1 in Scientific Notation: 3.88E-4

MB1 in Decimal Notation: 0.000388

MB2 in Scientific Notation: 3.7002563E-4

MB2 in Decimal Notation: 0.0003700256

like image 96
Andrea Ligios Avatar answered Oct 10 '22 23:10

Andrea Ligios


This is the way Java(and many other languages) display floating point numbers. E just means 10^, so 3.88E-4 is another way of writing 3.88 x 10^-4, which is the same as 0.000388.

This format is called scientific notation. E-notation is a computer representation of scientific notation.

The rest of your inaccuracy (388 vs 311) is because 1000000 is not the exact number you want to be dividing by. See the other answers for more details.

like image 33
Oleksi Avatar answered Oct 11 '22 01:10

Oleksi