1
0
mirror of https://github.com/mastodon/mastodon-ios.git synced 2024-12-08 14:46:42 +01:00
mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/MastodonExtension/Int.swift

32 lines
898 B
Swift
Raw Normal View History

//
// 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)
}
}
}