24 lines
456 B
Swift
24 lines
456 B
Swift
//
|
|
// ByteFormatting.swift
|
|
// iKeyMon
|
|
//
|
|
// Created by tracer on 01.04.25.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension Int {
|
|
func toNiceBinaryUnit() -> String {
|
|
let units = ["B", "KB", "MB", "GB", "TB", "PB"]
|
|
var value = Double(self)
|
|
var index = 0
|
|
|
|
while value >= 1024 && index < units.count - 1 {
|
|
value /= 1024
|
|
index += 1
|
|
}
|
|
|
|
return String(format: "%.2f %@", value, units[index])
|
|
}
|
|
}
|