how to convert Arabic numbers to English numbers in swift?

帅比萌擦擦* 提交于 2019-12-12 11:23:50

问题


in my app, I have 5 uitextfields. My intended users are Arabic speakers so when they use my app, they will either insert number in English or in Arabic.

The app works just fine with English numbers. However, when I run it and insert numbers in Arabic, the app deals with the textfields as if they all were empty and gives the answer as 0

How can I make sure that the textfields are not nill AND a double, whithout ignoring Arabic numbers?

This is the code inside the "calButton" function:

     /*let value1 = Double(income.text ?? "") ?? 0
    let value2 = Double(salaries.text ?? "") ?? 0
    let value3 = Double(tools.text ?? "") ?? 0
    let value4 = Double(maintinance.text ?? "") ?? 0
    let value5 = Double(otherExpenses.text ?? "") ?? 0
    let sum = value1 - ( value2 + value3 + value4 + value5)

    print("result is: \(sum)")*/



    let textFields = [income, salaries, tools, maintinance, otherExpenses]
    var sum = 0.0
    for textField in textFields {
        if let number = Double((textField?.text!)!) { //checks that it is not nil AND a Double
            sum += number
        }
    }

    expensesTotal.text = String(sum)
    // print("result is: \(sum)")

回答1:


You need to convert the arabic number string to english first and then do the calculation part.

    let numberStr: String = "٨٦٩١٢٨٨١"
    let formatter: NumberFormatter = NumberFormatter()
    formatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
    let final = formatter.number(from: numberStr)
    let doubleNumber = Double(final!)
    print("\(doubleNumber)")



回答2:


Swift-5 solution for all non-Latin string occurrences.

extension String {
    var containsNonEnglishNumbers: Bool {
        return !isEmpty && range(of: "[^0-9]", options: .regularExpression) == nil
    }

    var english: String {
        return self.applyingTransform(StringTransform.toLatin, reverse: false) ?? self
    }
}

Usage -

textField.addTarget(self, action: #selector(didChangeText(field:)), for: .editingChanged)

@objc func didChangeText(field: UITextField) {
    if ((field.text?.containsNonEnglishNumbers) != nil) {
        field.text = field.text?.english
    }
}



回答3:


This will replace any arabic number to english

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField.keyboardType == .numberPad && string != "" {
            let numberStr: String = string
            let formatter: NumberFormatter = NumberFormatter()
            formatter.locale = Locale(identifier: "EN")
            if let final = formatter.number(from: numberStr) {
                textField.text =  "\(textField.text ?? "")\(final)"
            }
                return false
        }
        return true
    }

dont forget setting the delegate:

yourTextField.delegat = self



来源:https://stackoverflow.com/questions/41668231/how-to-convert-arabic-numbers-to-english-numbers-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!