Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert arabic number to english number

I could convert English numbers to Arabic numbers in Xcode. but now I want to convert Arabic/Persian numbers to English numbers in iOS ...

Please guide me about this...

This is my code for conversion (English to Arabic) :

- (NSString*)convertEnNumberToFarsi:(NSString*)number {
  NSString *text;
  NSDecimalNumber *someNumber = [NSDecimalNumber decimalNumberWithString:number];
  NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
  NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"fa"];
  [formatter setLocale:gbLocale];
  text = [formatter stringFromNumber:someNumber];
  return text;
}
like image 628
Mohammad Kasiri Avatar asked Feb 12 '15 11:02

Mohammad Kasiri


People also ask

How can I convert Arabic number to English in Swift?

I have a simpler solution with String Extension: extension String{ /// To convert Arabic / Persian numbers to English /// - Returns: returns english number func changeToEnglish()-> String{ let numberFormatter = NumberFormatter() numberFormatter. locale = Locale(identifier: "EN") let engNumber = numberFormatter.

Are Arabic numbers the same as English?

Thus, reading numbers in Arabic is no different from English. This should give you one less thing to worry about when managing your Arabic source text.


2 Answers

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
like image 123
Mohammad Kasiri Avatar answered Sep 19 '22 12:09

Mohammad Kasiri


You must take care of not only Persian numbers, but also Arabic ones.

Use the below functions/methods to do so:

// Convert string From English numbers to Persian numbers
+(NSString *) convertToPersianNumber:(NSString *) string {
    NSNumberFormatter *formatter = [NSNumberFormatter new];
    formatter.locale = [NSLocale localeWithLocaleIdentifier:@"fa"];
    for (NSInteger i = 0; i < 10; i++) {
        NSNumber *num = @(i);
        string = [string stringByReplacingOccurrencesOfString:num.stringValue withString:[formatter stringFromNumber:num]];
    }
    return string;
}

// Convert string From Arabic/Persian numbers to English numbers
+(NSString *) convertToEnglishNumber:(NSString *) string {
    NSNumberFormatter *formatter = [NSNumberFormatter new];
    formatter.locale = [NSLocale localeWithLocaleIdentifier:@"fa"];
    for (NSInteger i = 0; i < 10; i++) {
        NSNumber *num = @(i);
        string = [string stringByReplacingOccurrencesOfString:[formatter stringFromNumber:num] withString:num.stringValue];
    }

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

    return string;
}
like image 45
Soheil Novinfard Avatar answered Sep 22 '22 12:09

Soheil Novinfard