Last active
December 27, 2018 15:54
-
-
Save Thunderbird7/3eb80d1f0535d58ea5ea to your computer and use it in GitHub Desktop.
[Swift] Abbreviate number class, such as number of 1500 convert to 1.5k
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
func abbreviateNumber(num: NSNumber) -> String { | |
// less than 1000, no abbreviation | |
if num < 1000 { | |
return "\(num)" | |
} | |
// less than 1 million, abbreviate to thousands | |
if num < 1000000 { | |
var n = Double(num); | |
n = Double( floor(n/100)/10 ) | |
return "\(n.description)k" | |
} | |
// more than 1 million, abbreviate to millions | |
var n = Double(num) | |
n = Double( floor(n/100000)/10 ) | |
return "\(n.description)m" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment