public String size(int size){
String hrSize = \"\";
int k = size;
double m = size/1024;
double g = size/1048576;
double t = size/1073741824;
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.