如何将阿拉伯数字转换为英文数字?

时间:2018-03-23 03:56:15

标签: ios swift

我有一个UITextField,大多数用户都有iPhone中的阿拉伯语和英语键盘。我的问题是,当我在文本字段中键入阿拉伯数字时,应用程序崩溃但是当我输入英文数字时,一切正常!

我的代码:

func plussNumbers() {
    let numberFormatter: NumberFormatter = NumberFormatter()
    numberFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
    var englishString = numberFormatter.number(from: "١")
    englishString = "1"

    textlable.numberOfLines = 0
    TextLable1.numberOfLines = 0


    if TextFailde.text == ""  {
        textlable.text! = textlable.text! + "\n"   +  TextFailde.text!
    }
    else {
        textlable.text! = textlable.text! + "\n" +  TextFailde.text!
        self.lblZeroUs.text! =  String(CInt(self.lblZeroUs.text!)! + CInt(self.TextFailde.text!)!) // i got error here
    }


    if TextFilde1.text == "" {
        TextLable1.sizeToFit()
        TextLable1.text! = TextLable1.text! + "\n"  +  TextFilde1.text!
    }
    else {


        TextLable1.text! = TextLable1.text! + "\n" + TextFilde1.text!
        self.lblZeroThey.text =  String(CInt(self.lblZeroThey.text!)! + CInt(self.TextFilde1.text!)!) // i got error here
    }

    previousValues.append(TextFailde.text ?? String(0))
    previousValues1.append(TextFilde1.text ?? String(0))
}

3 个答案:

答案 0 :(得分:0)

试试这个

dim MyPath as string = "C:\MyData\source\Chr"
dim MyPath as string = Application.StartupPath & "\source\Chr" 'Alternative
dim MyFile as string = "1YZ-C01C 567.737.061-eeprom.txt"
If My.Computer.FileSystem.FileExists(Path.Combine(MyPath,MyFile)) then
...
end if

示例:let numberFormatter: NumberFormatter = NumberFormatter() numberFormatter.locale = Locale(identifier: "en") let englishString = numberFormatter.number(from: YourArabicString)

英文字符串= arabicString = "٣"

答案 1 :(得分:0)

从阿拉伯数字转换后的englishString是可选的,需要添加可选的绑定。

let numberFormatter: NumberFormatter = NumberFormatter()
numberFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
if let englishString = numberFormatter.number(from: "٤") {
    print(englishString)
}

答案 2 :(得分:-1)

// creating a number (3-4 digit number) from english to farsi e.g. 3456 -> ????
class func numbersToArabic(num : String) -> String{
let digitsArr =  Array(num)
var arabicNum = ""
for i in 0 ..< digitsArr.count{
let digit = digitsArr[i]
let char: String = "\(digit)".localize
            print(char)
            arabicNum.append(char)
        }
        return arabicNum
    }
@just reverse the logic to obtain english to arabic
make sure that localization is there and 
the number you want to obtain are in locatized file
0 -> (arabic number)
1 -> (arabic number)
2 -> (arabic number)

thanks, happy coding