Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何存储0.0001352?_Java_Format_Double_Decimal - Fatal编程技术网

Java 如何存储0.0001352?

Java 如何存储0.0001352?,java,format,double,decimal,Java,Format,Double,Decimal,我有一个类,它有一个双'sizeinMegs'变量,当值很小时,比如说0.00025兆,它只存储0.0,就像在调试表中看到的那样 我怎样才能把它保存在十进位的荣耀中呢 这是我的密码: thePdf.setFileSizeInMegaBytes((theFile.length() / 1000000)); //that's a double double size = theFile.length(); // this returns say 12345

我有一个类,它有一个双'sizeinMegs'变量,当值很小时,比如说0.00025兆,它只存储0.0,就像在调试表中看到的那样

我怎样才能把它保存在十进位的荣耀中呢

这是我的密码:

        thePdf.setFileSizeInMegaBytes((theFile.length() / 1000000)); //that's a double

        double size = theFile.length(); // this returns say 12345
        size = size / 1000000; this returns 0.012345
        double storedValue = thePdf.getFileSizeInMegaBytes(); // this shows 0.0 in the watch window

        String value;
        value = fmt(size); //this shows the right value in the string

        lblTest.setText("Size: " +  value + " MB"); 

 ....

public static String fmt(double d)
{
    if(d == (int) d)
        return String.format("%d",(int)d);
    else
        return String.format("%s",d);
}
你应该使用双精度而不是双精度,这样你就不会失去精度。 您可以在以下位置显示代码:

thePdf.setFileSizeInMegaBytes()


这里很难说,因为我看不到“theFile”的声明,但是如果length()方法返回一个long或int值,那么程序的第一行就是整数/长除法,因此任何小于1的值都变为0

如果这里是这种情况,请先将长度转换为双倍

第二部分工作的原因是该声明具有从int/long到double的隐式强制转换,因此任何后续计算都会导致double


加倍大小=文件长度()

您的问题是整数除法。而不是

theFile.length() / 1000000
它总是向下四舍五入,你可以这样写

theFile.length() / 1000000.0
这迫使它成为一个非整数除法

无论何时将
/
放在
int
类型的两个表达式之间,结果也将是
int
,并且它将被舍入。

谢谢您的帮助:)下面是它们的设置方式:public double getFileSizeInBytes(){return m_db_FileSizeInBytes;}public void setFileSizeInBytes(double value){m_db_FileSizeInBytes=value;}
theFile.length() / 1000000.0