如何快速将阿拉伯数字转换为英文数字? [英] how to convert Arabic numbers to English numbers in swift?
问题描述
在我的应用程序中,我有 5 个 uitextfields.我的目标用户是阿拉伯语使用者,因此当他们使用我的应用时,他们会插入英语或阿拉伯语数字.
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.
该应用程序可以很好地处理英文数字.但是,当我运行它并以阿拉伯语插入数字时,该应用程序将文本字段处理为好像它们都为空并给出答案为 0
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
如何在不忽略阿拉伯数字的情况下确保文本字段不是 nill 和 double ?
How can I make sure that the textfields are not nill AND a double, whithout ignoring Arabic numbers?
这是calButton"函数内部的代码:
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)")
推荐答案
需要先将阿拉伯数字字符串转换成英文,然后再做计算部分.
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)")
这篇关于如何快速将阿拉伯数字转换为英文数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!