I tried to test the following code in order to get a double value from a currency-style formatted UITextField (ex : $ 30,034.12 => 30034.12) :
// Init and configure formatter
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setMaximumFractionDigits:2];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"us_US"];
[formatter setLocale:locale];
[formatter setGroupingSize:3];
[formatter setUsesGroupingSeparator:YES];
[formatter setGroupingSeparator:@","];
[formatter setAllowsFloats:YES];
// Get a formatted string using the local currency formatter and then get a double
// vice-versa
NSNumber *amount = [NSNumber numberWithDouble:30034.12];
NSString *amountString = [formatter stringFromNumber:amount];
// Output here OK : @"$ 30,034.12"
double amountDouble = [[formatter numberFromString:amountString] doubleValue];
// Output here NOT OK : 0
Did anyone have/solved the same problem ?
Thanks !
Update :
@DanielBarden and the others. Actually to simplify the post I didn't include the part specifying where I got my string from : a text field. In fact, the line before the end of the code should be :
NSString *amountString = [textField text];
And this text field was previously formatted with following code from another method (currency style using the same formatter configuration) :
// Init fromatter with style $ XXX,XXX.yy
NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setMaximumFractionDigits:2];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"us_US"];
[formatter setLocale:locale];
[formatter setGroupingSize:3];
[formatter setGroupingSeparator:@","];
[formatter setUsesGroupingSeparator:YES];
// Get the float value of the text field and format it
textField.text = [formatter
stringFromNumber:[NSNumber
numberWithDouble:[textField.text
floatValue]]];
Now the problem is that I get exactly the same strings when I do an NSLog but when I compare them char by char with a loop, it says that the space after the $ is a "real space" on the text field and differente symbol on the initial string (amountString the one I tried to test with in the initial post ...). Encoding issue ?
The code appears correct to me and Xcode agreed with me. I added in two lines that you are missing:
and the output was correct. I suggest you check how you are logging the values.