Skip to content

Instantly share code, notes, and snippets.

@Thunderbird7
Last active December 27, 2018 15:54
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thunderbird7/3eb80d1f0535d58ea5ea to your computer and use it in GitHub Desktop.
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
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