Cocoa NSNumberFormatterCurrencyStyle without "$" returns zero

I have a formatting format configured to convert currency strings to decimal values. The problem is that if there is no dollar sign ("$") in the text string, it will be converted to 0, and not to a valid corresponding number. So:

"$3.50" converts to 3.50 "3.50" converts to 0 

Here is the code for the converter:

 // formatter to convert a value to and from a currency string NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init]; [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle]; [currencyFormatter setGeneratesDecimalNumbers:YES]; 

Did I miss something?

+3
source share
2 answers

I had a similar problem. I ended up abandoning NSNumberFormatter for parsing and switching to RegexKit Lite with much better results.

Take a copy here: RegexKit Lite

Then go here http://regexlib.com/ and find the regex that suits you best.

+1
source

It converts "3.50" to 0 because it does not recognize "3.50" as a valid currency string, as indicated here: http://blog.clickablebliss.com/2006/10/05/the-challenges-of-international-money -in-cocoa / . If the default NSNumberFormatterCurrencyStyle is not flexible enough for your needs, you may need to subclass to handle these cases.

+1
source

All Articles