how to convert arabic number to english number
Asked Answered
W

3

7

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;
}
Wahoo answered 12/2, 2015 at 11:10 Comment(0)
W
12

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
Wahoo answered 12/2, 2015 at 11:19 Comment(1)
NSNumberFormatter *Formatter = [[NSNumberFormatter alloc] init]; NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"EN"]; [Formatter setLocale:locale]; NSNumber *newNum = [Formatter numberFromString:str]; if (newNum) { NSLog(@"%@", newNum); } str = [NSString stringWithFormat:@"%@",newNum]; NSString *firstCH = [str substringWithRange:NSMakeRange(0,1)]; if ([firstCH isEqualToString:@"0"]) { return [NSString stringWithFormat:@"%@",newNum]; }else{ return [NSString stringWithFormat:@"0%@",newNum];Club
L
9

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;
}
Leasehold answered 2/3, 2016 at 12:58 Comment(0)
H
0

Follow the same. Just replace your locale identifier with "en".

Helpless answered 12/2, 2015 at 12:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.