ios - 如何快速将阿拉伯数字转换为英文数字?

标签 ios swift uitextfield

在我的应用程序中,我有 5 个 uitextfields。我的目标用户是说阿拉伯语的人,因此当他们使用我的应用时,他们会插入英语或阿拉伯语的数字。

该应用程序可以很好地处理英文数字。但是,当我运行它并用阿拉伯语插入数字时,该应用程序处理文本字段,就好像它们都是空的一样,并给出答案 0

如何在不忽略阿拉伯数字的情况下确保文本字段不是 null 和 double?

这是“calButton”函数中的代码:

     /*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)")

最佳答案

您需要先将阿拉伯数字字符串转换为英文,然后再进行计算部分。

    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)")

关于ios - 如何快速将阿拉伯数字转换为英文数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41668231/

相关文章:

ios - 从 FCM 向 iOS 设备发送 2-layer-json-payload 时出现问题

swift - PWA 中的额外边距 - 无法拉伸(stretch)整个高度

ios - 错误域=NSCocoaErrorDomain 代码=3840 "No value."UserInfo={NSDebugDescription=无值。}

uitextfield - -[UITextFieldLabel setTextColor :] 中的断言失败

ios - 如果 TextField 为空,则禁用按钮

ios - Objective C 整数数学错误 (2200 - 100 = 1800) - 不知道为什么

ios - 从 nsuserdefaults 存储和检索值时发生冲突

ios - 对 CAReplicatorLayer 实例应用不同的效果

iphone - UITextField 动画完成,但在用户触摸后又弹回到原来的位置

ios - 将 CAShapeLayer 应用于除 UIButton 之外的主 UIView