TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

[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      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.