iOS Currency input field
I would like to have a currency input field in my app where the user can
either include the currency symbol (as appropriate for their locale), or
not, as they please.
I have a text field set up and I am storing the value in a NSDecimalNumber
(which I understand is the recommended way to store currency).
The following code will get me from an NSDecimalNumber to a formatted
currency string:
[NSNumberFormatter localizedStringFromNumber:currencyValue
numberStyle:NSNumberFormatterCurrencyStyle]
But I can't find a way to do the reverse of that. i.e., take the string
that the user has typed into my text field and convert it (if possible)
into an NSDecimalNumber. Keeping in mind that the currency symbol may be
there (because it came from the function above) or not (because the user
didn't bother to type the currency symbol).
What am I missing?
If I can't figure this out I will just not accept any currency symbol at
all (i.e., just parse it using the code below). But it seems better to
allow the currency symbol.
[NSDecimalNumber decimalNumberWithString:currencyString locale:[NSLocale
currentLocale]]
I have the feeling I am missing something. What's the right way to convert
back and forth between a localized currency string and an NSDecimalNumber?
No comments:
Post a Comment