Created
April 5, 2018 12:25
-
-
Save kobeumut/f380df7d1bfd23bbb5a5754dcd047f23 to your computer and use it in GitHub Desktop.
You are printing a number not a string Xcode 9 • Swift 4
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
| 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