kiwix-apple/SwiftUI/Model/Formatter.swift
Emmanuel Engelhart d6c23073aa Add GPL headers
2024-04-05 18:20:18 +02:00

71 lines
2.1 KiB
Swift

/*
* This file is part of Kiwix for iOS & macOS.
*
* Kiwix is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* any later version.
*
* Kiwix is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kiwix; If not, see https://www.gnu.org/licenses/.
*/
//
// Formatter.swift
// Kiwix
//
// Created by Chris Li on 6/28/22.
// Copyright © 2022 Chris Li. All rights reserved.
//
import Foundation
enum Formatter {
static let dateShort: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .none
return formatter
}()
static let dateMedium: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
return formatter
}()
static let size: ByteCountFormatter = {
let formatter = ByteCountFormatter()
formatter.countStyle = .file
return formatter
}()
static let number: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
return formatter
}()
static let percent: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .percent
return formatter
}()
static func largeNumber(_ value: Int64) -> String {
let sign = ((value < 0) ? "-" : "" )
let abs = Swift.abs(value)
guard abs >= 1000 else {return "\(sign)\(abs)"}
let exp = Int(log10(Double(abs)) / log10(1000))
let units = ["K", "M", "G", "T", "P", "E"]
let rounded = round(10 * Double(abs) / pow(1000.0, Double(exp))) / 10
return "\(sign)\(rounded)\(units[exp-1])"
}
}