-
Notifications
You must be signed in to change notification settings - Fork 0
/
Int+Extensions.swift
159 lines (143 loc) · 5.12 KB
/
Int+Extensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// Int+Extensions.swift
// OYExtensions
//
// Created by osmanyildirim
//
import Foundation
extension Int {
/// let value: Int = 3
/// `value.oy_string` → output → (String) "3"
public var oy_string: String {
return String(self)
}
/// let value: Int = 3
/// `value.oy_float` → output → (CGFloat) 3.0
public var oy_cgFloat: CGFloat {
return CGFloat(self)
}
/// let value: Int = 3
/// `value.oy_string` → output → (Double) 3.0
public var oy_double: Double {
return Double(self)
}
/// let value: Int = 3
/// `value.oy_float` → output → (Float) 3.0
public var oy_float: Float {
return Float(self)
}
/// let value: Int = 314
/// `value.oy_digits` → output → [3, 1, 4]
public var oy_digits: [Int] {
guard self != 0 else { return [0] }
var digits = [Int]()
var number = abs(self)
while number != 0 {
let digit = number % 10
digits.append(digit)
number /= 10
}
digits.reverse()
return digits
}
/// let value: Int = 314
/// `value.oy_digitsCount` → output → 3
public var oy_digitsCount: Int {
guard self != 0 else { return 1 }
let number = Double(abs(self))
return Int(log10(number) + 1)
}
/// let value: Int = 2
/// `value.oy_isEven` → output → true
public var oy_isEven: Bool {
self % 2 == 0
}
/// let value: Int = 2
/// `value.oy_isOdd` → output → false
public var oy_isOdd: Bool {
!oy_isEven
}
/// `0.oy_format` → output → "0"
/// `100.oy_format` → output → "100"
/// `-20000.oy_format` → output → -2K
/// `1000000.oy_format` → output → 1M
/// `-2000000000.oy_format` → output → -2B
public var oy_format: String {
let abs = abs(Double(self))
let sign = (self < 0) ? "-" : ""
switch abs {
case 100_000_000_000...:
var formattedDouble = abs / 1_000_000_000
formattedDouble = FormatHelper.reduceScale(double: formattedDouble, places: 1)
let formatted = Int(formattedDouble)
return "\(sign)\(formatted)B"
case 1_000_000_000...:
var formattedDouble = abs / 1_000_000_000
formattedDouble = FormatHelper.reduceScale(double: formattedDouble, places: 1)
let noDecimal = (floor(formattedDouble) == formattedDouble)
if noDecimal {
let formatted = Int(formattedDouble)
return "\(sign)\(formatted)B"
} else {
return "\(sign)\(formattedDouble)B"
}
case 100_000_000...:
var formattedDouble = abs / 1_000_000
formattedDouble = FormatHelper.reduceScale(double: formattedDouble, places: 1)
let formatted = Int(formattedDouble)
return "\(sign)\(formatted)M"
case 1_000_000...:
var formattedDouble = abs / 1_000_000
formattedDouble = FormatHelper.reduceScale(double: formattedDouble, places: 1)
let noDecimal = (floor(formattedDouble) == formattedDouble)
if noDecimal {
let formatted = Int(formattedDouble)
return "\(sign)\(formatted)M"
} else {
return "\(sign)\(formattedDouble)M"
}
case 100_000...:
var formattedDouble = abs / 1_000
formattedDouble = FormatHelper.reduceScale(double: formattedDouble, places: 1)
let formatted = Int(formattedDouble)
return "\(sign)\(formatted)K"
case 1_000...:
var formattedDouble = abs / 1_000
formattedDouble = FormatHelper.reduceScale(double: formattedDouble, places: 1)
let noDecimal = (floor(formattedDouble) == formattedDouble)
if noDecimal {
let formatted = Int(formattedDouble)
return "\(sign)\(formatted)K"
} else {
return "\(sign)\(formattedDouble)K"
}
case 0...:
let formattedNumber = FormatHelper.decimalFormatter.string(from: NSNumber(value: self)) ?? ""
return "\(formattedNumber)"
default:
let formattedNumber = FormatHelper.decimalFormatter.string(from: NSNumber(value: self)) ?? ""
return "\(sign)\(formattedNumber)"
}
}
/// `5.oy_times { time in
/// ` print(time) // 1, 2, 3, 4, 5
/// `}
public func oy_times(_ completion: (Int) throws -> Void) rethrows {
guard self > 0 else { return }
for i in 1 ... self { try completion(i) }
}
}
extension Int {
private struct FormatHelper {
static let decimalFormatter: NumberFormatter = {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
return numberFormatter
}()
static func reduceScale(double: Double, places: Double) -> Double {
let multiplier = pow(10, places)
let truncated = Double(multiplier * double)
return truncated / multiplier
}
}
}