I have written a function to convert a number from arabic numerals to digits. I have used NSNumberFormatter
for this. But the problem is the number caqn start with a zero and NSNumberFormatter
truncates the leading zero. I need this value as I have to do a comparison. The number might not start with zero always. So I cannot put any other condition. Pleaes help me if anyone has a solution. Thank you.
My code is:
-(NSString*)convertNumberFromArabic:(NSString*)numberToConvert{
NSNumberFormatter *nf1 = [[NSNumberFormatter alloc] init];
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en"];
[nf1 setLocale:locale];
NSNumber *newNum = [nf1 numberFromString:numberToConvert];
NSString *reverseConvert = [nf1 stringFromNumber:newNum];
return reverseConvert;
}
Your question is a bit confusing. You say you want to "convert a number from arabic numerals to digits", but your method name says to Arabic. I have written the method to convert from Arabic to Latin. If you want to got the other way, you just change the YES
to NO
.
Here's the code:
-(NSString*)convertArabicDigitsToLatin:(NSString*)numberToConvert{
NSMutableString* result = [numberToConvert mutableCopy];
CFRange range = CFRangeMake(0, result.length);
CFStringTransform((__bridge CFMutableStringRef)result, &range, kCFStringTransformLatinArabic, YES);
return result;
}
This doesn't interpret the string as a number, it just transliterates the Arabic digits as string characters to Latin digits.