humanSize static method

String humanSize(
  1. int size
)

Returns a human readable string of a file size, e.g. '2.389MB'.

Implementation

static String humanSize(int size) {
  String rc;
  String unit;
  if (size < 1000) {
    rc = size.toString() + 'B';
  } else {
    double size2;
    if (size < 1000000) {
      unit = 'KB';
      size2 = size / 1000.0;
    } else if (size < 1000000000) {
      unit = 'MB';
      size2 = size / 1000000.0;
    } else if (size < 1000000000000) {
      unit = 'GB';
      size2 = size / 1000000000.0;
    } else {
      unit = 'TB';
      size2 = size / 1000000000000.0;
    }
    rc = sprintf('%.3f%s', [size2, unit]);
  }
  return rc;
}