toFileSize method

String? toFileSize({
  1. int decimals = 0,
})

将字节转为容量单位

Implementation

String? toFileSize({int decimals = 0}) {
  if (this == null) {
    return null;
  }
  if (this == 0) {
    return '0 B';
  }
  const suffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
  var i = (log(this!) / log(1024)).floor();
  return '${(this! / pow(1024, i)).toStringAsFixed(decimals)} ${suffixes[i]}';
}