java 长浮点数输出显示字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13363429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:27:00  来源:igfitidea点击:

Long float-number output shows letters

javafloating-point

提问by Birdman

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.

方法 fileList[i].length() 返回 311 个字节作为 Long 类型。

The previous code results in the following output:

前面的代码产生以下输出:

3.88E-4

3.88E-4

How do I get my expected output of 0,000311 inside the fileLengthMegabytes variable?

如何在 fileLengthMegabytes 变量中获得预期的 0,000311 输出?

回答by Andrea Ligios

That is Scientific Notation.

那就是科学记数法。

AND you are getting 388 instead of 311 because you are dividing by 1000000 instead of 1048576 (1024 * 1024)

并且您得到的是 388 而不是 311,因为您除以 1000000 而不是 1048576 (1024 * 1024)

EDIT: 311 is not achieved even with 1048576, that way you get 370... so the error is probably in your calc ;)

编辑:即使使用 1048576 也没有达到 311,这样你就会得到 370 ......所以错误可能在你的计算中;)

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

运行示例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

科学记数法中的 MB1:3.88E-4

十进制表示法中的 MB1:0.000388

科学记数法中的 MB2:3.7002563E-4

十进制表示的 MB2:0.0003700256

回答by Oleksi

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

这是 Java(和许多其他语言)显示浮点数的方式。E只是意味着10^,所以3.88E-4是另一种写作方式,3.88 x 10^-40.000388.

This format is called scientific notation. E-notationis 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.

其余的不准确(388 对 311)是因为 1000000 不是您想要除以的确切数字。有关更多详细信息,请参阅其他答案。

回答by Dmytro Chyzhykov

3.88E-4 as mentioned before is a representation in the scientific E-notation.

前面提到的 3.88E-4 是科学 E-notation 中的表示

If you want to print it or convert to String you can write something like that:

如果你想打印它或转换为 String 你可以写这样的东西:

System.out.printf("%f%n", fileLengthMegabytes);
// or
String fileLengthMegabytesMessage = String.format(
        "File size is %fMb", fileLengthMegabytes);
System.out.println(fileLengthMegabytesMessage);

and you've got:

你有:

0,000311
// or 
File size is 0,000311Mb

respectfully

尊敬

回答by Sri Harsha Chilakapati

If you want normal numbers (without scientific notation), use BigInteger

如果您想要正常数字(没有科学计数法),请使用 BigInteger

BigInteger inbytes = new BigInteger("" + fileList[i].length());
BigInteger inMB = inBytes.divide(new BigInteger("" + (1024*1024)));

Note: Normal doublevalue would also suffice.

注意:正常值double也足够了。

回答by HerrB

To get the notation you want, use a formatter:

要获得您想要的符号,请使用格式化程序:

final Formatter f = new Formatter();
final String out = f.format("%8.6f", fileLengthMegabytes);

should give you your result.

应该给你你的结果。

See also: Java Doc: String.Format, Java Doc: Formatter

另请参阅: Java 文档:String.FormatJava 文档:格式化程序