public static String formatBytes ( long bytes ) { double value = bytes ; String unit = "B" ; if ( bytes >= 1024 ) { int exponent = ( int ) ( Math . log ( bytes ) / Math . log ( 1024 ) ) ; exponent = Math . min ( exponent , 4 ) ; switch ( exponent ) { case 1 : unit = "KB" ; break ; case 2 : unit = "MB" ; break ; case 3 : unit = "GB" ; break ; case 4 : unit = "TB" ; break ; } value = bytes / Math . pow ( 1024 , exponent ) ; } DecimalFormat df = new DecimalFormat ( "#.##" ) ; return df . format ( value ) + " " + unit ; }
Format the bytes into readable text