Skip to content

Instantly share code, notes, and snippets.

@denandreychuk
Created August 17, 2021 21:13
Show Gist options
  • Select an option

  • Save denandreychuk/49d6a0b2cd421a34cbc61b8229135b90 to your computer and use it in GitHub Desktop.

Select an option

Save denandreychuk/49d6a0b2cd421a34cbc61b8229135b90 to your computer and use it in GitHub Desktop.
Как сделать аббревиатуру для числа? | https://t.me/BlogSwift
// СВИФТЕР | Блог про 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