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

4

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

Cloudberry answered 16/5, 2017 at 5:34 Comment(4)
see this my answer : https://mcmap.net/q/2029533/-ios-swift-numberformatter-decimal-style-localizationDefensive
thanks This was usefulCloudberry
duplicate: stackoverflow.com/questions/30479225Satisfy
Where are you calling convertEngNumToPersianNum?Shingly
D
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!)
Defensive answered 16/5, 2017 at 5:45 Comment(2)
please help me to find similar question answer #44628482Cloudberry
can you elaborate what is your requirement .Defensive
K
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 ?? "")
Kokanee answered 16/5, 2017 at 5:49 Comment(0)
O
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)
        }
Ottoman answered 11/10, 2017 at 10:34 Comment(0)
A
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
}
}
Akeylah answered 3/6, 2020 at 21:40 Comment(5)
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.Lowspirited
@Lowspirited thanks for your comment but about digits Persian history is older than Arabic and math move from Persian empire to Arabic region.Akeylah
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.Lowspirited
@Lowspirited 6 has two form(۶ and ٦) in IR-FA and i put both of them to show there aren't different between themAkeylah
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: en.wikipedia.org/wiki/… . 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.Lowspirited

© 2022 - 2024 — McMap. All rights reserved.