mastodon-ios/MastodonSDK/Sources/MastodonExtension/Int.swift

32 lines
898 B
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Int.swift
//
//
// Created by Marcus Kida on 28.12.22.
//
import Foundation
public extension Int {
func asAbbreviatedCountString() -> String {
switch self {
case ..<1_000:
return String(format: "%d", locale: Locale.current, self)
case 1_000 ..< 999_999:
return String(format: "%.1fK", locale: Locale.current, Double(self) / 1_000)
.sanitizedAbbreviatedCountString(for: "K")
default:
return String(format: "%.1fM", locale: Locale.current, Double(self) / 1_000_000)
.sanitizedAbbreviatedCountString(for: "M")
}
}
}
fileprivate extension String {
func sanitizedAbbreviatedCountString(for value: String) -> String {
[".0", ",0", "٫٠"].reduce(self) { res, acc in
return res.replacingOccurrences(of: "\(acc)\(value)", with: value)
}
}
}