UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

[Project 1] UIKeyboardType.decimalPad has a comma instead of a dot on a regional keyboard

Forums > 100 Days of SwiftUI

Hi everyone!

I’m currently working on the first project.

When the device keyboard is set to some languages (including mine, French (Switzerland)), the keyboard has a comma (“,”) instead of a dot (“.”). When I read the content of the TextField, the checkAmount string contains a comma instead of a dot.

The issue is that on the other hand, the Double initializer doesn’t recognize this separator. For instance, Double("12,34") fails while Double("12.34") does not.

Would there be a way to force the keyboard to use the English format with a dot?

I was thinking about manually replacing commas by dots before calling the Double initializer, but I’m afraid that it could still be broken in other languages I’m now aware of.

Here’s a screenshot showing the issue on the simulator:

Simulator screenshot

2      

Hi Nicolas,

You can do so by using the default String operator .replacingOccurrences(of: String, with: String).

By doing so, you will be able to type "," as well as "." in your textfiel, without any trouble.

Here is a complete SwiftUI example to do so. Hope this will help you.

Regards Patrick

struct InputView: View {

    @State private var textInput = ""

    func converter(text: String) -> String {
        let textDouble = Double(textInput.replacingOccurrences(of: ",", with: ".")) ?? 0
        // If the Textfield is empty, 0 will be returned
        return String(format: "%.2f", textDouble)
    }

    var body: some View {
        Form {
            Section(header: Text("Input")) {
                TextField("Type in a Bool", text: $textInput)
            }
            .keyboardType(.decimalPad)

            Section(header: Text("Output")) {
                Text("$\(converter(text: textInput))")
            }
        }
    }
}

struct InputView_Previews: PreviewProvider {
    static var previews: some View {
        InputView()
    }
}

3      

Hi guys,

Since i'm in Switzerland too, I went through the same issue :)

Thanks @patchlock for the tip !

The answer is here if others come across :

  1. The function must be added before the body
    func converter(text: String) -> String {
        let conversion = Double(**checkAmount**.replacingOccurrences(of: ",", with: ".")) ?? 0
        return String(format: "%.2f", conversion)
    }
  1. The function must be called like this :
    var totalPerPerson: Double {
        let peopleCount = Double(numberOfPeople + 2)
        let TipSelection = Double(tipPercentages[tipPercentage])
        let orderAmount = Double(converter(text: checkAmount)) ?? 0

        let tipAmount = orderAmount / 100 * TipSelection
        let grandTotal = orderAmount + tipAmount
        let amountPerPerson = grandTotal / peopleCount

        return amountPerPerson
    }

:)

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.