Convert Arabic String to English Number in Swift

how to convert Arabic numbers to English numbers in swift?

You need to convert the arabic number string to english first and then do the calculation part.

    let numberStr: String = "٨٦٩١٢٨٨١"
let formatter: NumberFormatter = NumberFormatter()
formatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let final = formatter.number(from: numberStr)
let doubleNumber = Double(final!)
print("\(doubleNumber)")

Convert Arabic String to english number in Swift

do like

     let NumberStr: String = "٢٠١٨-٠٦-٠٤"
let Formatter = NumberFormatter()
Formatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
if let final = Formatter.number(from: NumberStr) {
print(final)

}

output

Sample Image

the alternate way

Option 2

extension String {
public var arToEnDigits : String {
let arabicNumbers = ["٠": "0","١": "1","٢": "2","٣": "3","٤": "4","٥": "5","٦": "6","٧": "7","٨": "8","٩": "9"]
var txt = self
arabicNumbers.map { txt = txt.replacingOccurrences(of: $0, with: $1)}
return txt
}
}

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

The simple way:

NSDictionary *numbersDictionary = @{@"1" : @"۱", @"2" : @"۲", @"3" : @"۳", @"4" : @"۴", @"5" : @"۵", @"6" : @"۶", @"7" : @"۷", @"8" : @"۸", @"9" : @"۹",@"0" : @"٠"};
for (NSString *key in numbersDictionary) {
str = [str stringByReplacingOccurrencesOfString:key withString:numbersDictionary[key]];
}

Other solution more flexible with locale:

NSNumberFormatter *formatter = [NSNumberFormatter new];
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"ar"];
for (NSInteger i = 0; i < 10; i++) {
NSNumber *num = @(i);
str = [str stringByReplacingOccurrencesOfString:num.stringValue withString:[formatter stringFromNumber:num]];
}

Note: this code wrote without IDE, it can be with syntax errors.

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

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

how to convert arabic number to english number

Try this, I hope this helps you :

NSString *NumberString = @"۸۸۸";
NSNumberFormatter *Formatter = [[NSNumberFormatter alloc] init];
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"EN"];
[Formatter setLocale:locale];
NSNumber *newNum = [Formatter numberFromString:NumberString];
if (newNum) {
NSLog(@"%@", newNum);
}
//print in console 888

How to split string as English and non English using Swift 4?

You can use a Natural Language Tagger, which would work even if both scripts are intermingled:

import NaturalLanguage

let str = "¿como? بداية start وسط middle начать средний конец نهاية end. 從中間開始. "

let tagger = NLTagger(tagSchemes: [.script])

tagger.string = str

var index = str.startIndex
var dictionary = [String: String]()
var lastScript = "other"

while index < str.endIndex {
let res = tagger.tag(at: index, unit: .word, scheme: .script)
let range = res.1

let script = res.0?.rawValue

switch script {
case .some(let s):
lastScript = s
dictionary[s, default: ""] += dictionary["other", default: ""] + str[range]
dictionary.removeValue(forKey: "other")
default:
dictionary[lastScript, default: ""] += str[range]
}

index = range.upperBound
}

print(dictionary)

and print the result if you'd like:

for entry in dictionary {
print(entry.key, ":", entry.value)
}

yielding :

Hant : 從中間開始. 
Cyrl : начать средний конец
Arab : بداية وسط نهاية
Latn : ¿como? start middle end.

This is still not perfect since the language tagger only checks to which script the most number of letters in a word belong to. For example, in the string you're working with, the tagger would consider الصَّالِحِينَ.Bismika as one word. To overcome this, we could use two pointers and traverse the original string and check the script of words individually. Words are defined as contiguous letters:

let str = "بِاسْمِكَ رَبِّي وَضَعْتُ جَنْبِي، وَبِكَ أَرْفَعُهُ، فَإِنْ أَمْسَكْتَ نَفْسِي فَارْحَمْهَا، وَإِنْ أَرْسَلْتَهَا فَاحْفَظْهَا، بِمَا تَحْفَظُ بِهِ عِبَادَكَ الصَّالِحِينَ.Bismika rabbee wadaAAtu janbee wabika arfaAAuh, fa-in amsakta nafsee farhamha, wa-in arsaltaha fahfathha bima tahfathu bihi AAibadakas-saliheen. In Your name my Lord, I lie down and in Your name I rise, so if You should take my soul then have mercy upon it, and if You should return my soul then protect it in the manner You do so with Your righteous servants."

let tagger = NLTagger(tagSchemes: [.script])
var i = str.startIndex
var dictionary = [String: String]()
var lastScript = "glyphs"

while i < str.endIndex {
var j = i
while j < str.endIndex,
CharacterSet.letters.inverted.isSuperset(of: CharacterSet(charactersIn: String(str[j]))) {
j = str.index(after: j)
}
if i != j { dictionary[lastScript, default: ""] += str[i..<j] }
if j < str.endIndex { i = j } else { break }

while j < str.endIndex,
CharacterSet.letters.isSuperset(of: CharacterSet(charactersIn: String(str[j]))) {
j = str.index(after: j)
}

let tempo = String(str[i..<j])
tagger.string = tempo
let res = tagger.tag(at: tempo.startIndex, unit: .word, scheme: .script)

if let s = res.0?.rawValue {
lastScript = s
dictionary[s, default: ""] += dictionary["glyphs", default: ""] + tempo
dictionary.removeValue(forKey: "glyphs")
}
else { dictionary["other", default: ""] += tempo }

i = j
}

How to convert arabic date to english date

For an easier conversion I think it's better to remove the additional text
so I filtered the string and kept only the needed numbers

here is my attempt:

var dateString = "الإثنين 2015.5.5 - 16:16 مساء  بتوقيت ابوظبي"

dateString = dateString.trimmingCharacters(in: CharacterSet(charactersIn: "01234567890.-:").inverted)

var formatter = DateFormatter()
formatter.dateFormat = "yyyy.MM.dd - HH:mm"

let date = formatter.date(from: dateString)

print(date)

//"May 5, 2015 at 4:16 PM"


Related Topics



Leave a reply



Submit