5

I read the similar questions here and Write this method in my app

        let formatter = NumberFormatter()

    func convertEngNumToPersianNum(num: String)->String{
        let number = NSNumber(value: Int(num)!)
        let format = NumberFormatter()
        format.locale = Locale(identifier: "fa_IR")
        let faNumber = format.string(from: number)

        return faNumber!


    }

I didn't get Error But I didn't get the result too!

my Number code is this :

let checkNumber = Home2ViewController().customtitle.count
    personalCustom.text = ("\(checkNumber)")

I used another Number in another View Controller that works But I want to show this Number in persian or arabic number format not in English format

Saeed Rahmatolahi
  • 1,317
  • 2
  • 27
  • 60

4 Answers4

7

Try this :

func convertEngNumToPersianNum(num: String)->String{
        //let number = NSNumber(value: Int(num)!)
        let format = NumberFormatter()
        format.locale = Locale(identifier: "fa_IR") 
        let number =   format.number(from: num)
        let faNumber = format.string(from: number!)
        return faNumber!

    }

OR repalce with your line

        let number =   format.number(from: num)
    let faNumber = format.string(from: number!)
KKRocks
  • 8,222
  • 1
  • 18
  • 84
2

You can do something like,

    let formatter = NumberFormatter()
    formatter.locale = NSLocale.current   // you can specify locale that you want
    formatter.numberStyle = .decimal
    formatter.usesGroupingSeparator = true



    let number = formatter.number(from: "١٠.٠٠")

    print(number ?? "")
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
1

To convert to Arabic while keeping the leading zeros

func convertToArDigits(_ digits: String) -> String {
            // We need a CFMutableString and a CFRange:
            let cfstr = NSMutableString(string: digits) as CFMutableString
            var range = CFRange(location: 0, length: CFStringGetLength(cfstr))
            // Do the transliteration (this mutates `cfstr`):
            CFStringTransform(cfstr, &range, kCFStringTransformLatinArabic, false)
            // Convert result back to a Swift string:
            return (cfstr as String)
        }
Kassem Itani
  • 1,057
  • 10
  • 15
1
   extension String {
public var faToEnDigits : String {
    let farsiNumbers = ["٠": "0","١": "1","٢": "2","٣": "3","٤": "4","٥": "5","٦": "6","٧": "7","٨": "8","٩": "9"]
    var txt = self
    farsiNumbers.map { txt = txt.replacingOccurrences(of: $0, with: $1)}
    return txt
}
public var enToFaDigits : String {
    let englishNumbers = ["0": "۰","1": "۱","2": "۲","3": "۳","4": "۴","5": "۵","6": "۶","7": "۷","8": "۸","9": "۹"]
    var txt = self
    englishNumbers.map { txt = txt.replacingOccurrences(of: $0, with: $1)}
    return txt
}
}
Masoud Roosta
  • 455
  • 9
  • 19
  • The digits you have used in your third line (`faToEnDigits`), are Arabic digits, not Persian, where those in `enToFaDigits` are actually Persian. This creates inconsistency in your code and conversion process. – Neeku Nov 02 '20 at 19:42
  • @Neeku thanks for your comment but about digits Persian history is older than Arabic and math move from Persian empire to Arabic region. – Masoud Roosta Nov 02 '20 at 19:52
  • So, what’s your point? “۴۵۶” vs. “٤٥٦” are 6 different unicode characters, the former three are for fa_IR, where the latter are for ar_AR. This has nothing to do to Persian Empire. In fact they used cuneiform which is an extinct ancient language. – Neeku Nov 02 '20 at 20:15
  • @Neeku 6 has two form(۶ and ٦) in IR-FA and i put both of them to show there aren't different between them – Masoud Roosta Nov 02 '20 at 20:20
  • 1
    Well, that is exactly the mistake I'm attempting to point out. They *are* indeed different. The former is U+06F6, the latter is U+0666. You can read more about that here: https://en.wikipedia.org/wiki/Persian_alphabet#Deviations_from_the_Arabic_script . You can also google them (or any other similar string like ی ک vs. ي ك in double quotations to see the different search results. (كيك كي vs. کیک کی) are actually different. This misuse of wrong characters impacts things like hashtag trends, etc. – Neeku Nov 02 '20 at 20:29