Search code examples
iosswift3number-formattingnsformatter

The NSFormatter for Converting English Number to persian or arabic Number in Swift 3


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


Solution

  • 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!)