static public String getFormattedBytes ( long bytes , String units , String format ) { double num = bytes ; String [ ] prefix = { "" , "K" , "M" , "G" , "T" } ; int count = 0 ; while ( num >= 1024.0 ) { num = num / 1024.0 ; ++ count ; } DecimalFormat f = new DecimalFormat ( format ) ; return f . format ( num ) + " " + prefix [ count ] + units ; }
Returns the given bytes number formatted as KBytes MBytes or GBytes as appropriate .