Skip to content

Instantly share code, notes, and snippets.

@kobeumut
Created April 5, 2018 12:25
Show Gist options
  • Save kobeumut/f380df7d1bfd23bbb5a5754dcd047f23 to your computer and use it in GitHub Desktop.
Save kobeumut/f380df7d1bfd23bbb5a5754dcd047f23 to your computer and use it in GitHub Desktop.
You are printing a number not a string Xcode 9 • Swift 4
extension String {
var twoFractionDigits: String {
let styler = NumberFormatter()
styler.minimumFractionDigits = 2
styler.maximumFractionDigits = 2
styler.numberStyle = .currency
let converter = NumberFormatter()
converter.decimalSeparator = "."
if let result = converter.number(from: self) {
return styler.string(for: result) ?? ""
}
return ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment