如何显示多个小数点的结果

时间:2013-04-13 22:24:49

标签: java

我试图显示一个结果,但它只是返回0,即使你计算答案它不是零但是在零之后需要更多的小数点

代码:

                long transferRate = ((len*2) / durationseconds ) ; //amount of data in bytes transferred in 1 second. Currently returning 0 for every result
                System.out.println("transferRate: " + transferRate + " bytes per second");

是我需要返回多个小数位的答案的部分,我该如何指定?

因为目前它正在显示结果:

Time for the ping to be sent and recived of 2 bytes is 365636931 seconds
transferRate: 0 bytes per second
Time for the ping to be sent and recived of 4 bytes is 43375591 seconds
transferRate: 0 bytes per second
Time for the ping to be sent and recived of 8 bytes is 51079641 seconds
transferRate: 0 bytes per second
Time for the ping to be sent and recived of 16 bytes is 54751211 seconds
transferRate: 0 bytes per second
Time for the ping to be sent and recived of 32 bytes is 57195731 seconds
transferRate: 0 bytes per second
Time for the ping to be sent and recived of 64 bytes is 48524461 seconds
transferRate: 0 bytes per second
Time for the ping to be sent and recived of 128 bytes is 51243251 seconds
transferRate: 0 bytes per second

由于

1 个答案:

答案 0 :(得分:3)

long transferRate = ((len*2) / durationseconds ) ;

您无法在long中存储小数。尝试:

double transferRate = ((len*2) / (double) durationseconds ) ;

您必须将其中一个操作数强制转换为double,否则您仍然会进行整数除法。

例如,1 / 2将评估为0。但是,1 / 2.0将是0.5