4

In iOS I'd like to perform some specific functions if the user's [NSLocale currentLocale] does not use Arabic Numerals (i.e. not 0123456789). How can I check for this condition?

MattyG
  • 8,449
  • 6
  • 44
  • 48

1 Answers1

6

Unfortunately there is no direct attribute to check, but the following code will check it:

NSCharacterSet *nSet = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];
NSNumberFormatter *numFmt = [[NSNumberFormatter alloc] init];
NSDecimalNumber *value = [NSDecimalNumber decimalNumberWithString:@"1234567890"];
NSCharacterSet *fSet = [NSCharacterSet characterSetWithCharactersInString:[numFmt stringFromNumber:value]];
if([fSet isSupersetOfSet:nSet]==NO){
    NSLog(@"No arabic digits");
}

The check is done via a set of the digits (nSet) against the locale aware formatter of the current locale (numFmt). The value is used to format a number of all digits, which could be reduced to less digits than 1 to 0. However this might serve as an example of other detections as well. Please note that using a separator or thousand grouping character will not break the detection because superset is being used.

iOS
  • 813
  • 5
  • 14
  • @Volatil3 You might want to start a new question to explain the specifics of your case. But initially I'd say that everything is handled for you. Use NSString's -floatValue or -intValue to get the number from your UITextField, and process the maths as normal. Then display your result in localized form by using NSNumberFormatter's -localizedStringFromNumber – MattyG Aug 01 '12 at 03:12
  • @Volatil3 right, but using the Formatter to parse would be the best option. – iOS Aug 02 '12 at 11:18