Skip to content

Instantly share code, notes, and snippets.

@bagpack
Created October 11, 2017 04:16
Show Gist options
  • Save bagpack/16d218fc464d6f287a3a32d16035b831 to your computer and use it in GitHub Desktop.
Save bagpack/16d218fc464d6f287a3a32d16035b831 to your computer and use it in GitHub Desktop.
ex) 9999 -> "9,999"
extension Int {
func decimal() -> String {
struct Static { // swiftlint:disable:this nesting cache formatter
static var formatter: NumberFormatter! = nil
}
if Static.formatter == nil {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.groupingSeparator = ","
formatter.groupingSize = 3
Static.formatter = formatter
}
return Static.formatter.string(from: NSNumber(value: self))!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment