Comma in Decimal number input - Locales

#1

Hi there,

thanks for the great book though. However, I am encountering a problem where I use my numbered keyboard, but it displays a comma instead of a point. Thus, my application does not work with decimals :slight_smile:

I have been googling but did not find a fast and easy solution. Any Help appreciated!

Swift3 Code btw:

var fahrenheitValue: Double? {
    didSet {
        updateCelsiusLabel()
    }
}

var celsiusValue:  Double? {
    if let value = fahrenheitValue {
        return (value - 32) * (5/9)
    } else {
        return nil
    }
    
}

func updateCelsiusLabel() {
    if let value = celsiusValue {
        // My guess is that this is the wrong part
        celsiusLabel.text = numberFormatter.string(from: NSDecimalNumber(value: value))
    } else {
        celsiusLabel.text = "???"
    }
}

let numberFormatter: NumberFormatter = {
    let nf = NumberFormatter()
    nf.numberStyle = NumberFormatter.Style.decimal
    nf.locale = NSLocale.current
    nf.minimumFractionDigits = 0
    nf.maximumFractionDigits = 1
    return nf
}()

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let existingTextHasDecimalSeparator = textField.text?.range(of: ".")
    let replacementTextHasDecimalSeparator = string.range(of: ".")
    
    if existingTextHasDecimalSeparator != nil && replacementTextHasDecimalSeparator != nil {
        return false
    }
    
    return true
}

Cheers!