public static String toHumanSize(long bytesValue){
  if (0 == bytesValue) {
    return "0B";
  }
  boolean powerOfTwo=false;
  if ((bytesValue & (bytesValue - 1L)) == 0) {
    powerOfTwo=true;
  }
  long multiple=(long)(bytesValue / 1.5);
  if ((multiple & (multiple - 1L)) == 0) {
    powerOfTwo=true;
  }
  Unit unit=Unit.B;
  if (powerOfTwo) {
    if (bytesValue >= Unit.EiB.value) {
      unit=Unit.EiB;
    }
 else     if (bytesValue >= Unit.PiB.value) {
      unit=Unit.PiB;
    }
 else     if (bytesValue >= Unit.TiB.value) {
      unit=Unit.TiB;
    }
 else     if (bytesValue >= Unit.GiB.value) {
      unit=Unit.GiB;
    }
 else     if (bytesValue >= Unit.MiB.value) {
      unit=Unit.MiB;
    }
 else     if (bytesValue >= Unit.KiB.value) {
      unit=Unit.KiB;
    }
  }
 else {
    if (bytesValue >= Unit.EB.value) {
      unit=Unit.EB;
    }
 else     if (bytesValue >= Unit.PB.value) {
      unit=Unit.PB;
    }
 else     if (bytesValue >= Unit.TB.value) {
      unit=Unit.TB;
    }
 else     if (bytesValue >= Unit.GB.value) {
      unit=Unit.GB;
    }
 else     if (bytesValue >= Unit.MB.value) {
      unit=Unit.MB;
    }
 else     if (bytesValue >= Unit.KB.value) {
      unit=Unit.KB;
    }
  }
  double val=(bytesValue / unit.getValue());
  double roundedValue=Math.round(val * 10.0) / 10.0;
  BigDecimal myDecimal=BigDecimal.valueOf(roundedValue);
  BigDecimal updated=myDecimal.stripTrailingZeros();
  return updated.toPlainString() + unit.name();
}

public static String toHumanSize ( long bytesValue ) { if ( 0 == bytesValue ) { return "0B" ; } boolean powerOfTwo = false ; if ( ( bytesValue & ( bytesValue - 1L ) ) == 0 ) { powerOfTwo = true ; } long multiple = ( long ) ( bytesValue / 1.5 ) ; if ( ( multiple & ( multiple - 1L ) ) == 0 ) { powerOfTwo = true ; } Unit unit = Unit . B ; if ( powerOfTwo ) { if ( bytesValue >= Unit . EiB . value ) { unit = Unit . EiB ; } else if ( bytesValue >= Unit . PiB . value ) { unit = Unit . PiB ; } else if ( bytesValue >= Unit . TiB . value ) { unit = Unit . TiB ; } else if ( bytesValue >= Unit . GiB . value ) { unit = Unit . GiB ; } else if ( bytesValue >= Unit . MiB . value ) { unit = Unit . MiB ; } else if ( bytesValue >= Unit . KiB . value ) { unit = Unit . KiB ; } } else { if ( bytesValue >= Unit . EB . value ) { unit = Unit . EB ; } else if ( bytesValue >= Unit . PB . value ) { unit = Unit . PB ; } else if ( bytesValue >= Unit . TB . value ) { unit = Unit . TB ; } else if ( bytesValue >= Unit . GB . value ) { unit = Unit . GB ; } else if ( bytesValue >= Unit . MB . value ) { unit = Unit . MB ; } else if ( bytesValue >= Unit . KB . value ) { unit = Unit . KB ; } } double val = ( bytesValue / unit . getValue ( ) ) ; double roundedValue = Math . round ( val * 10.0 ) / 10.0 ; BigDecimal myDecimal = BigDecimal . valueOf ( roundedValue ) ; BigDecimal updated = myDecimal . stripTrailingZeros ( ) ; return updated . toPlainString ( ) + unit . name ( ) ; }
convert a value in bytes to value readable by humans
