3

I've found this excellent question / answer on how localize a string with the users current currency settings... localize-currency-for-iphone

This is the code used...

NSDecimalNumber *someAmount = [NSDecimalNumber decimalNumberWithString:@"5.00"];
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSLog(@"%@", [currencyFormatter stringFromNumber:someAmount]);

However, I need to convert this to a double so I can store it to my database?

EDIT: That should read I need to convert this back to a string without the currency formating, then convert it to a double.

Community
  • 1
  • 1
Jules
  • 7,568
  • 14
  • 102
  • 186
  • Using double for currency is not a good idea because converting back and forth between base 10 and base 2 can cause a loss of precision called "base conversion error". It's better to deal with decimal numbers in base 10 strings, and instead of converting to base 2 doubles, it's best to convert base 10 strings to base 10 NSDecimal numbers. Doing this saves the precision of the number and prevents such errors as printing customer statements for a penny. – Kaydell Nov 19 '17 at 02:36

1 Answers1

3

Take the string and call the reverse method:

NSNumber* number = [currencyFormatter numberFromString:string];
Aviad Ben Dov
  • 6,351
  • 2
  • 34
  • 45