public static String toUnitbytes(long bytes){
  if (bytes < 0) {
    return "? " + GENERAL_UNIT_KILOBYTES;
  }
  long unitValue;
  String unitName;
  if (bytes < 0xA00000) {
    unitValue=0x400;
    unitName=GENERAL_UNIT_KILOBYTES;
  }
 else   if (bytes < 0x280000000L) {
    unitValue=0x100000;
    unitName=GENERAL_UNIT_MEGABYTES;
  }
 else   if (bytes < 0xA0000000000L) {
    unitValue=0x40000000;
    unitName=GENERAL_UNIT_GIGABYTES;
  }
 else {
    unitValue=0x10000000000L;
    unitName=GENERAL_UNIT_TERABYTES;
  }
  NumberFormat numberFormat;
  if ((double)bytes * 100 / unitValue < 99995)   numberFormat=NUMBER_FORMAT1;
 else   numberFormat=NUMBER_FORMAT0;
  try {
    return numberFormat.format((double)bytes / unitValue) + " " + unitName;
  }
 catch (  ArithmeticException ae) {
    return "0 " + unitName;
  }
}

public static String toUnitbytes ( long bytes ) { if ( bytes < 0 ) { return "? " + GENERAL_UNIT_KILOBYTES ; } long unitValue ; String unitName ; if ( bytes < 0xA00000 ) { unitValue = 0x400 ; unitName = GENERAL_UNIT_KILOBYTES ; } else if ( bytes < 0x280000000L ) { unitValue = 0x100000 ; unitName = GENERAL_UNIT_MEGABYTES ; } else if ( bytes < 0xA0000000000L ) { unitValue = 0x40000000 ; unitName = GENERAL_UNIT_GIGABYTES ; } else { unitValue = 0x10000000000L ; unitName = GENERAL_UNIT_TERABYTES ; } NumberFormat numberFormat ; if ( ( double ) bytes * 100 / unitValue < 99995 ) numberFormat = NUMBER_FORMAT1 ; else numberFormat = NUMBER_FORMAT0 ; try { return numberFormat . format ( ( double ) bytes / unitValue ) + " " + unitName ; } catch ( ArithmeticException ae ) { return "0 " + unitName ; } }
converts the passed in number of bytes into a byte - size string . group digits with locale - dependant thousand separator if needed , but with " kb " , or " mb " or " gb " or " tb " locale - dependant unit at the end , and a limited precision of 4 significant digits .
