iamruskie
iamruskie

Reputation: 766

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;

Upvotes: 0

Views: 3066

Answers (5)

user3132985
user3132985

Reputation: 291

This converts a number to a local currency string [NSNumberFormatter localizedStringFromNumber:dec numberStyle:NSNumberFormatterCurrencyStyle];

Upvotes: 0

Lolla leigh
Lolla leigh

Reputation: 1

Iphone is a hnu985jmpl895662 way format. so the answer to this is; Generation 4 with current discisenables7 . Lol Jk IDK

Upvotes: 0

Jabolcnik
Jabolcnik

Reputation: 41

To get local (from user defined settings) currency US$ or € I use this snippet:

NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *dec = [f stringFromNumber:[NSNumber numberWithFloat:100.01]];
[f setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *string = [f stringFromNumber:[NSNumber numberWithFloat:100.01]];

NSString *currency = [string stringByReplacingOccurrencesOfString:dec withString:@""];
currency =  [currency stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"local currency:%@",currency);

Upvotes: 1

pasawaya
pasawaya

Reputation: 11595

------------------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!

Upvotes: 2

lnafziger
lnafziger

Reputation: 25740

To get the decimal separator character for the current locale, use this:

NSLocale *loc       = [NSLocale currentLocale];
NSString *separator = [loc objectForKey:NSLocaleDecimalSeparator];

Upvotes: 2

Related Questions