Search code examples
iphonexcodexcode4

Detect regional currency format (iPhone)


Okay so I have an app with a numberfield (buttons with values 0-9) that updates a label which is formatted to the local currency. The label automatically places the decimal for the user, so the format is 0.00. Now this is a problem because iphone formats most other currencies, such as the Euro, as 0,00.

Is there a way to detect the regional currency format (Settings > International > Region Format)?

I would then make an if statement accordingly.

My current number pad code:

NSString *digit = sender.currentTitle;
numberField.text = [numberField.text stringByAppendingString:digit];

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setGroupingSeparator:@""];
[numberFormatter setMaximumIntegerDigits:4];
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setMaximumFractionDigits:2];

numberField.text = [numberField.text stringByReplacingOccurrencesOfString:@"."    withString:@""];
NSDecimalNumber *currency = [[NSDecimalNumber decimalNumberWithString:numberField.text]   decimalNumberByDividingBy: [NSDecimalNumber decimalNumberWithString:@"100"]];
numberField.text = [numberFormatter stringFromNumber:currency];

You can see on line 11 where it inserts the decimal. When i change it to a comma, the 0,00 format works properly.

And the conversion:

NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *string = [f stringFromNumber:[NSNumber numberWithFloat:[numberField.text floatValue]]];

numberField.text=string;

Solution

  • ------------------EDIT------------------

    I believe that @lnafziger probably has a more efficient way to do it. In addition to the NSLocaleDecimalSeparator key, you might also find the NSLocaleGroupingSeparator and NSLocaleCurrencySymbol keys useful.

    Hope this helps!

    You could use:

    NSString *locale = [NSLocale currentLocale] localeIdentifier];
    if(locale isEqualToString: @"en_US"){
    
        //Set Device Currency to American Dollars
    
    }
    else if(locale isEqualToString: @"fr_FR"){
    
        //Set Device Currency to Euros
    
    }
    else if(locale isEqualToString: @"en_AU"){
    
        //Set Device Currency to Australian Dollars
    
    }
    

    For more info on NSLocale, see here.

    Hope this helps!