mirror of
https://github.com/immich-app/immich.git
synced 2026-02-15 05:18:37 +03:00
25 lines
579 B
Dart
25 lines
579 B
Dart
import 'dart:math';
|
|
|
|
extension NumberToSizeExtension on num {
|
|
String formatAsSize({int noOfDecimals = 0}) {
|
|
const List<String> units = [
|
|
'B',
|
|
'KB',
|
|
'MB',
|
|
'GB',
|
|
'TB',
|
|
'PB',
|
|
'EB',
|
|
'ZB',
|
|
'YB',
|
|
];
|
|
if (this == 0) return '0 B';
|
|
final index = (log(this) / log(1024)).floor();
|
|
final byteIndex = index.clamp(0, units.length - 1);
|
|
|
|
final size = (this / pow(1024, byteIndex)).round();
|
|
// ignore: avoid-unsafe-collection-methods
|
|
return '${size.toStringAsFixed(noOfDecimals)} ${units[byteIndex]}';
|
|
}
|
|
}
|