我可以在Xcode中将英文数字转换为阿拉伯数字。但现在我想在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;
}
答案 0 :(得分:12)
试试这个,我希望这会对你有所帮助:
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
答案 1 :(得分:8)
你不仅要照顾波斯数字,还要注意阿拉伯数字。
使用以下功能/方法:
// 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;
}
答案 2 :(得分:0)
遵循同样的原则。只需将您的区域设置标识符替换为"en"
。