feat: moved from binary to metric based size units to have the same calculation as macOS (#2230)

This commit is contained in:
Serhiy Mytrovtsiy
2024-12-17 14:13:58 +01:00
parent 8a99997f86
commit 3887157a7c
4 changed files with 50 additions and 35 deletions

View File

@@ -132,16 +132,16 @@ public struct Units {
}
public var kilobytes: Double {
return Double(bytes) / 1_024
return Double(bytes) / 1_000
}
public var megabytes: Double {
return kilobytes / 1_024
return kilobytes / 1_000
}
public var gigabytes: Double {
return megabytes / 1_024
return megabytes / 1_000
}
public var terabytes: Double {
return gigabytes / 1_024
return gigabytes / 1_000
}
public func getReadableTuple(base: DataSizeBase = .byte) -> (String, String) {
@@ -149,15 +149,15 @@ public struct Units {
let multiplier: Double = base == .byte ? 1 : 8
switch bytes {
case 0..<1_024:
case 0..<1_000:
return ("0", "K\(stringBase)/s")
case 1_024..<(1_024 * 1_024):
case 1_000..<(1_000 * 1_000):
return (String(format: "%.0f", kilobytes*multiplier), "K\(stringBase)/s")
case 1_024..<(1_024 * 1_024 * 100):
case 1_000..<(1_000 * 1_000 * 100):
return (String(format: "%.1f", megabytes*multiplier), "M\(stringBase)/s")
case (1_024 * 1_024 * 100)..<(1_024 * 1_024 * 1_024):
case (1_000 * 1_000 * 100)..<(1_000 * 1_000 * 1_000):
return (String(format: "%.0f", megabytes*multiplier), "M\(stringBase)/s")
case (1_024 * 1_024 * 1_024)...Int64.max:
case (1_000 * 1_000 * 1_000)...Int64.max:
return (String(format: "%.1f", gigabytes*multiplier), "G\(stringBase)/s")
default:
return (String(format: "%.0f", kilobytes*multiplier), "K\(stringBase)B/s")
@@ -169,19 +169,19 @@ public struct Units {
let multiplier: Double = base == .byte ? 1 : 8
switch bytes*Int64(multiplier) {
case 0..<1_024:
case 0..<1_000:
let unit = omitUnits ? "" : " K\(stringBase)/s"
return "0\(unit)"
case 1_024..<(1_024 * 1_024):
case 1_000..<(1_000 * 1_000):
let unit = omitUnits ? "" : " K\(stringBase)/s"
return String(format: "%.0f\(unit)", kilobytes*multiplier)
case 1_024..<(1_024 * 1_024 * 100):
case 1_000..<(1_000 * 1_000 * 100):
let unit = omitUnits ? "" : " M\(stringBase)/s"
return String(format: "%.1f\(unit)", megabytes*multiplier)
case (1_024 * 1_024 * 100)..<(1_024 * 1_024 * 1_024):
case (1_000 * 1_000 * 100)..<(1_000 * 1_000 * 1_000):
let unit = omitUnits ? "" : " M\(stringBase)/s"
return String(format: "%.0f\(unit)", megabytes*multiplier)
case (1_024 * 1_024 * 1_024)...Int64.max:
case (1_000 * 1_000 * 1_000)...Int64.max:
let unit = omitUnits ? "" : " G\(stringBase)/s"
return String(format: "%.1f\(unit)", gigabytes*multiplier)
default:
@@ -190,17 +190,17 @@ public struct Units {
}
}
public func getReadableMemory() -> String {
public func getReadableMemory(to round: Int = 1) -> String {
switch bytes {
case 0..<1_024:
case 0..<1_000:
return "0 KB"
case 1_024..<(1_024 * 1_024):
case 1_000..<(1_000 * 1_000):
return String(format: "%.0f KB", kilobytes)
case 1_024..<(1_024 * 1_024 * 1_024):
case 1_000..<(1_000 * 1_000 * 1_000):
return String(format: "%.0f MB", megabytes)
case 1_024..<(1_024 * 1_024 * 1_024 * 1_024):
case 1_000..<(1_000 * 1_000 * 1_000 * 1_000):
return String(format: "%.1f GB", gigabytes)
case (1_024 * 1_024 * 1_024 * 1_024)...Int64.max:
case (1_000 * 1_000 * 1_000 * 1_000)...Int64.max:
return String(format: "%.1f TB", terabytes)
default:
return String(format: "%.0f KB", kilobytes)

View File

@@ -382,13 +382,13 @@ extension SizeUnit: CaseIterable {
public func toBytes(_ value: Int) -> Int {
switch self {
case .KB:
return value * 1_024
return value * 1_000
case .MB:
return value * 1_024 * 1_024
return value * 1_000 * 1_000
case .GB:
return value * 1_024 * 1_024 * 1_024
return value * 1_000 * 1_000 * 1_000
case .TB:
return value * 1_024 * 1_024 * 1_024 * 1_024
return value * 1_000 * 1_000 * 1_000 * 1_000
default:
return value
}