toLogicSizeString static method
Converts a file size in bytes to a human-readable string format.
This method converts the file size from bytes to the largest appropriate unit (KB, MB, GB) and formats the size to two decimal places. The returned string includes the unit of measurement.
For example:
500
bytes will be converted to'500 B'
1500
bytes will be converted to'1.46 KB'
1,500,000
bytes will be converted to'1.43 MB'
1,500,000,000
bytes will be converted to'1.40 GB'
fileSizeInBytes
is the file size in bytes to be converted.
Returns a String representing the file size in a human-readable format.
Implementation
static String toLogicSizeString(int fileSizeInBytes) {
if (fileSizeInBytes < 1024) {
return '$fileSizeInBytes B';
} else if (fileSizeInBytes < 1024 * 1024) {
double fileSizeInKB = fileSizeInBytes / 1024;
return '${fileSizeInKB.toStringAsFixed(2)} KB';
} else if (fileSizeInBytes < 1024 * 1024 * 1024) {
double fileSizeInMB = fileSizeInBytes / (1024 * 1024);
return '${fileSizeInMB.toStringAsFixed(2)} MB';
} else {
double fileSizeInGB = fileSizeInBytes / (1024 * 1024 * 1024);
return '${fileSizeInGB.toStringAsFixed(2)} GB';
}
}