public static void main(String[] args) throws InterruptedException{
    long sTime =  new Date().getTime();
    Thread.sleep(3234);
    long eTime =  new Date().getTime();
    float diff = ((eTime-sTime)/1000);
    System.out.println(diff);
}


在上面的代码中,我期望输出为3.234但它是3.0。我希望在几秒钟内两次之间有确切的差异,并有一个小数部分。

最佳答案

您正在执行积分除法而不是浮点除法。尝试这个:

float diff = ((float)(eTime-sTime)/1000.0);


当您使用long时,我进一步建议您使用double数据类型以提高精度:

double diff = ((double)(eTime-sTime)/1000.0);

09-27 09:26