123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- //
- // Units.swift
- // NexilisLite
- //
- // Created by Akhmad Al Qindi Irsyam on 20/02/23.
- //
- import Foundation
- public struct Units {
-
- public let bytes: Int64
-
- public var kilobytes: Double {
- return Double(bytes) / 1_024
- }
-
- public var megabytes: Double {
- return kilobytes / 1_024
- }
-
- public var gigabytes: Double {
- return megabytes / 1_024
- }
-
- public init(bytes: Int64) {
- self.bytes = bytes
- }
-
- public func getReadableUnit() -> String {
-
- switch bytes {
- case 0..<1_024:
- return "\(bytes) Bytes"
- case 1_024..<(1_024 * 1_024):
- return "\(String(format: "%.2f", kilobytes)) KB"
- case 1_024..<(1_024 * 1_024 * 1_024):
- return "\(String(format: "%.2f", megabytes)) MB"
- case (1_024 * 1_024 * 1_024)...Int64.max:
- return "\(String(format: "%.2f", gigabytes)) GB"
- default:
- return "\(bytes) Bytes"
- }
- }
- }
|