如何将阿拉伯数字转换为英文数字

how to convert arabic number to english number

我可以在 Xcode 中将英文数字转换为阿拉伯数字。但现在我想将 Arabic/Persian 数字转换为 iOS ...

中的英文数字

请指导我...

这是我的转换代码(英语到阿拉伯语):

- (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;
}

试试这个,希望对你有帮助:

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

同理。只需将您的区域设置标识符替换为 "en".

你不仅要注意波斯数字,还要注意阿拉伯数字。

使用下面的 functions/methods 这样做:

// 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;
}