如何将印度数转换为阿拉伯数?

Asi*_*Net 2 objective-c ios xcode5

我正试图从我的应用程序拨打电话,但似乎我不能,因为数字是印度格式(例如:966595848882)并使其工作,我必须将此字符串转换为阿拉伯语格式(例如:966595848882)

我的代码:

NSString *cleanedString = [[ContactInfo componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];

NSString *phoneNumber = [@"telprompt://" stringByAppendingString:cleanedString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Run Code Online (Sandbox Code Playgroud)

Tom*_*ton 6

使用NSNumberFormatter适当的区域设置.例如:

NSString *indianNumberString = @"????????????";
NSNumberFormatter *nf1 = [[NSNumberFormatter alloc] init];
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"hi_IN"];
[nf1 setLocale:locale];

NSNumber *newNum = [nf1 numberFromString:indianNumberString];
NSLog(@"new: %@", newNum);
Run Code Online (Sandbox Code Playgroud)

这打印"966595848882".

我不是100%肯定上面的语言环境标识符 - hi_IN应该是"印地语印度".如果这不正确,请使用[NSLocale availableLocaleIdentifiers]获取所有已知区域设置标识符的列表,并找到更合适的区域标识符.

更新:为了将其填充到九位数(或者您想要的数量),请转换回NSString使用标准NSString格式:

NSString *paddedString = [NSString stringWithFormat:@"%09ld", [newNum integerValue]];
Run Code Online (Sandbox Code Playgroud)

格式%09ld将零填充到九位数.

也可以使用相同的数字格式化器,将上面的数字转换回字符串,同时需要9位数字.这也至少提供九位数,如果需要,填充为零:

[nf1 setMinimumIntegerDigits:9];
NSString *reverseConvert = [nf1 stringFromNumber:newNum];
Run Code Online (Sandbox Code Playgroud)