mirror of
https://github.com/yattee/yattee.git
synced 2026-02-20 01:39:46 +00:00
Yattee v2 rewrite
This commit is contained in:
30
Yattee/Utilities/CountFormatter.swift
Normal file
30
Yattee/Utilities/CountFormatter.swift
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// CountFormatter.swift
|
||||
// Yattee
|
||||
//
|
||||
// Centralized utility for formatting counts with compact notation (1K, 2.5K, etc.)
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// Utility for formatting counts with compact notation and proper pluralization.
|
||||
enum CountFormatter {
|
||||
/// Formats a count to compact notation (e.g., 1K, 2.5K, 1M, 1B).
|
||||
/// - Parameter count: The number to format
|
||||
/// - Returns: Formatted string (e.g., "150", "1.2K", "2.5M", "1B")
|
||||
static func compact(_ count: Int) -> String {
|
||||
switch count {
|
||||
case 0..<1_000:
|
||||
return "\(count)"
|
||||
case 1_000..<1_000_000:
|
||||
let value = Double(count) / 1_000
|
||||
return String(format: "%.1fK", value).replacingOccurrences(of: ".0K", with: "K")
|
||||
case 1_000_000..<1_000_000_000:
|
||||
let value = Double(count) / 1_000_000
|
||||
return String(format: "%.1fM", value).replacingOccurrences(of: ".0M", with: "M")
|
||||
default:
|
||||
let value = Double(count) / 1_000_000_000
|
||||
return String(format: "%.1fB", value).replacingOccurrences(of: ".0B", with: "B")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user