Created
August 17, 2021 21:13
-
-
Save denandreychuk/49d6a0b2cd421a34cbc61b8229135b90 to your computer and use it in GitHub Desktop.
Как сделать аббревиатуру для числа? | https://t.me/BlogSwift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // СВИФТЕР | Блог про Swift | t.me/BlogSwift | |
| extension Int { | |
| var abbreviated: String { | |
| "KMBTQ".enumerated().reversed().reduce(nil as String?) { result, abbreviation in | |
| let factor = Double(self) / pow(10, Double(abbreviation.offset + 1) * 3) | |
| let format = (factor.truncatingRemainder(dividingBy: 1) == 0 ? "%.0f%@" : "%.1f%@") | |
| return result ?? (factor > 1 ? String(format: format, factor, String(abbreviation.element)) : nil) | |
| } ?? String(self) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment