Converting KB to MB, GB, TB dynamically

后端 未结 10 1955
广开言路
广开言路 2020-12-14 18:24
public String size(int size){
    String hrSize = \"\";
    int k = size;
    double m = size/1024;
    double g = size/1048576;
    double t = size/1073741824;

            


        
10条回答
  •  無奈伤痛
    2020-12-14 19:18

    The problem is that you're using integer division. Change your code to:

    double m = size/1024.0;
    double g = size/1048576.0;
    double t = size/1073741824.0;
    

    In your original code, double m = size/1024 would divide the integer size by 1024, truncate the result to an integer, and only then convert it to double. This is why the fractional part was getting lost.

提交回复
热议问题