Sometimes it is surprising how fast you don't get an answer any more since the problem become a bit difficult.
Sorry, but I didn’t notice your follow-up question. In future, you might think about putting separate questions in a separate thread, especially once you’ve marked the thread as resolved.
However, I fixed the problem myself by replacing and converting every "," to "."
There are easier ways to convert commas to dots. Here’s one that does it manually:
func convertCommasToDots(_ input: String) -> String {
return String(input.map {
$0 == "," ? "." : $0
})
}
but my preferred approach is to use the high-level API:
func convertCommasToDots(_ input: String) -> String {
return input.replacingOccurrences(of: ",", with: ".")
}
However, that’s not a great solution to this problem because there’s such a variety of different number formatting approaches around the world. The preferred option is to use
NumberFormatter
. Along those lines, you wrote:
I looked up in the Dev Documentation but unfortunately there is no example so I don't know how exactly implement this.
There is a lot of information about
NumberFormatter
out there, you just have to adjust your search parameters:
Much of Apple’s documentation hasn’t made the leap to the new documentation format. For example, Data Formatting Guide is full of useful tidbits about formatters. The code snippets are in Objective-C, not Swift, but the core concepts are all the same.
There’s a bunch of third-party resources out there. You’ll want to search for Swift NumberFormatter to prioritise Swift stuff, because many different environments use the name
NumberFormatter
for this. Also, make sure you only look at results from Swift 3 or later, because of the Swift Great Renaming.
Anyway, here’s a code snippet you get you started:
func numberFor(_ input: String) -> Double? {
let nf = NumberFormatter()
nf.numberStyle = .decimal
return nf.number(from: input)?.doubleValue
}
This will do the right thing based on the user’s current locale. For example, it works equally well in
en_GB
(English as used in the UK) locale:
print(numberFor("1")) // -> Optional(1.0)
print(numberFor("1.2")) // -> Optional(1.2)
print(numberFor("12345.6")) // -> Optional(12345.6)
print(numberFor("12,345.6")) // -> Optional(12345.6)
and the
ar_PK
(Arabic as used in Pakistan) locale:
let one = "\u{0661}"
print(numberFor(one)) // -> Optional(1.0)
let onePointTwo = "\u{0661}\u{066B}\u{0662}"
print(numberFor(onePointTwo)) // -> Optional(1.2)
let oneTwoThreeFourFivePointSix = "\u{0661}\u{0662}\u{0663}\u{0664}\u{0665}\u{066B}\u{0666}"
print(numberFor(oneTwoThreeFourFivePointSix)) // -> Optional(12345.6)
let oneTwoCommaThreeFourFivePointSix = "\u{0661}\u{0662}\u{066C}\u{0663}\u{0664}\u{0665}\u{066B}\u{0666}"
print(numberFor(oneTwoCommaThreeFourFivePointSix)) // -> Optional(12345.6)
[Annoyingly, DevForums won’t let me post Arabic characters, so I had to use Unicode escaping in my second code snippet )-: If you want to see this example in its full glory, print the strings using Swift and then copy’n’paste the results into string literals.]
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"