How to convert english numbers inside a string to Persian/Arabic numbers in Objective-C?

前端 未结 10 1985
轮回少年
轮回少年 2021-01-18 10:53

I have an english string that may or may not have numbers. But i want those numbers to be printed on screen as Persian numbers.

For example if NSString *foo =

10条回答
  •  伪装坚强ぢ
    2021-01-18 11:29

    convert number to any locale

    let formatter = NumberFormatter()
    formatter.locale = Locale(identifier: "ar")
    if let localized = formatter.string(from: NSNumber(value: comp.0)) {
       return "\(localized)"
    }
    

    example:

    (lldb) po formatter.string(from: NSNumber(value: 0)) ▿ Optional - some : "٠"

    (lldb) po formatter.string(from: NSNumber(value: 1)) ▿ Optional - some : "١"

    (lldb) po formatter.string(from: NSNumber(value: 2)) ▿ Optional - some : "٢"

    (lldb) po formatter.string(from: NSNumber(value: 3)) ▿ Optional - some : "٣"

    (lldb) po formatter.string(from: NSNumber(value: 4)) ▿ Optional - some : "٤"

    (lldb) po formatter.string(from: NSNumber(value: 5)) ▿ Optional - some : "٥"

    (lldb) po formatter.string(from: NSNumber(value: 6)) ▿ Optional - some : "٦"

    (lldb) po formatter.string(from: NSNumber(value: 7)) ▿ Optional - some : "٧"

    (lldb) po formatter.string(from: NSNumber(value: 8)) ▿ Optional - some : "٨"

    (lldb) po formatter.string(from: NSNumber(value: 9)) ▿ Optional - some : "٩"

    (lldb) po formatter.string(from: NSNumber(value: 10)) ▿ Optional - some : "١٠"

提交回复
热议问题