1
0
mirror of https://github.com/mastodon/mastodon-ios.git synced 2024-12-08 06:42:04 +01:00
mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/MastodonExtension/Int.swift
2022-12-28 11:59:24 +01:00

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