Skip to content

Instantly share code, notes, and snippets.

View snoozemoose's full-sized avatar

Mattias Persson snoozemoose

  • Panurgic AB
  • Linköping, Sweden
View GitHub Profile
@mminer
mminer / formatBytes.swift
Last active April 19, 2023 00:59
Formats bytes into a more human-readable form (e.g. MB).
import Foundation
func format(bytes: Double) -> String {
guard bytes > 0 else {
return "0 bytes"
}
// Adapted from http://stackoverflow.com/a/18650828
let suffixes = ["bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
let k: Double = 1000