public static String formatSize ( long size ) { if ( size == 0 ) return "0" ; StringBuilder sb = new StringBuilder ( ) ; if ( size < 0 ) { size = - size ; sb . append ( "-" ) ; } long gigs = size / ( 1024 * 1024 * 1024 ) ; if ( gigs > 0 ) { sb . append ( new DecimalFormat ( "######.###" ) . format ( ( double ) size / ( 1024 * 1024 * 1024 ) ) ) ; sb . append ( " GB" ) ; return sb . toString ( ) ; } long megs = size / ( 1024 * 1024 ) ; if ( megs > 0 ) { sb . append ( new DecimalFormat ( "###.#" ) . format ( ( double ) size / ( 1024 * 1024 ) ) ) ; sb . append ( " MB" ) ; return sb . toString ( ) ; } long kbs = size / ( 1024 ) ; if ( kbs > 0 ) { sb . append ( new DecimalFormat ( "###.#" ) . format ( ( double ) size / ( 1024 ) ) ) ; sb . append ( " KB" ) ; return sb . toString ( ) ; } sb . append ( size ) ; sb . append ( " B" ) ; return sb . toString ( ) ; }
Format the given size