aboutsummaryrefslogtreecommitdiff
path: root/lib/utils/file_utils.dart
blob: 4f3f0b745b3e421c7c2b3fe99774ea64ef7e7c18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import 'package:intl/intl.dart';

const kilo = 1024;
const mega = kilo * kilo;
const giga = mega * kilo;
const tera = giga * kilo;

String formatFileSize(String locale, int size, {int round = 2}) {
  if (size < kilo) return '$size B';

  final compactFormatter = NumberFormat('0${round > 0 ? '.${'0' * round}' : ''}', locale);
  if (size < mega) return '${compactFormatter.format(size / kilo)} KB';
  if (size < giga) return '${compactFormatter.format(size / mega)} MB';
  if (size < tera) return '${compactFormatter.format(size / giga)} GB';
  return '${compactFormatter.format(size / tera)} TB';
}