Skip to content

Instantly share code, notes, and snippets.

@ashtonmeuser
Created November 6, 2017 21:53
Show Gist options
  • Select an option

  • Save ashtonmeuser/2e88c0e9df7b4b8cbd5cbfa42a07d11e to your computer and use it in GitHub Desktop.

Select an option

Save ashtonmeuser/2e88c0e9df7b4b8cbd5cbfa42a07d11e to your computer and use it in GitHub Desktop.
Return a formatted score
//
// PrettyScore.swift
//
// Created by Ashton Meuser on 2017-07-21.
// Copyright © 2017 Ashton Meuser. All rights reserved.
//
import Foundation
extension Double {
func roundedTo(places: Int) -> Double {
let divisor = pow(10.0, Double(places))
return (self * divisor).rounded() / divisor
}
}
extension Int {
var prettyScore: String {
guard self != 0 else {
return "0"
}
var score = String(self)
switch abs(self) {
case 10000000..<100000000:
score = "\(Int(Double(self)/1000000.0))M"
case 1000000..<100000000:
score = "\((Double(self)/1000000.0).roundedTo(places: 1))M"
case 10000..<1000000:
score = "\(Int(Double(self)/1000.0))k"
case 1000..<10000:
score = "\((Double(self)/1000.0).roundedTo(places: 1))k"
default:
break
}
return "\(self > 0 ? "+" : "")\(score)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment