import Foundation enum BytesReading { static func read(_ bytes: Double) -> String? { guard bytes.isFinite, bytes >= 0, bytes.rounded() == bytes else { return nil } let units = ["B", "KB", "MB", "GB", "TB"] var value = bytes var unit = 0 while value >= 1000, unit < units.count - 1 { value /= 1000 unit += 1 } let pattern = unit > 0 && value < 10 ? "%.1f" : "%.0f" var figure = String(format: pattern, locale: Locale(identifier: "en_US_POSIX"), value) if figure.hasSuffix(".0") { figure.removeLast(2) } return figure + " " + units[unit] } }