Re: Numeric Entry and Formatting With NSNumberFormatter Won't Append Zeros
Re: Numeric Entry and Formatting With NSNumberFormatter Won't Append Zeros
- Subject: Re: Numeric Entry and Formatting With NSNumberFormatter Won't Append Zeros
- From: Kyle Sluder <email@hidden>
- Date: Wed, 28 Sep 2011 09:28:42 -0700
On Sep 27, 2011, at 5:33 PM, Philip McIntosh <email@hidden> wrote:
> I don't see anything wrong with pulling a string off a display, formatting it as a number, converting the number back into a string and redisplaying it. As I said the formatter works as expected with any number I give it starting out as a defined string (if my string is 1234567.000321 the formatter produces 1,234,567.000321). It is only when the string is built by appending digits that something goes wrong (no zeros accepted after a decimal point).
I think Fritz wanted to make sure that the canonical value of the user's input is stored in a numerical form (most likely an NSDecimalNumber) in your model layer, rather than relying on the string in your UI as the canonical value.
What you've describing could be interpreted either way. When entering text in the UI, I would expect the following to occur:
user input builds up a string in the UITextView
Return key pressed: UITextView string --(NSNumberFormatter)--> NSDecimalNumber stored in the model layer
Then immediately: NSDecimalNumber --(NSNumberFormatter)--> UITextView string, to update the UI to reflect the canonical value that was interpreted from user input, in case for example NSNumberFormatter saw an ambiguity and chose one possible interpretation.
All computations should be performed on the stored NSDecimalNumber, not by constantly reinterpreting the string value of the text view.
As for your actual use of NSNumberFormatter, you should never manually strip and parse decimal or thousands separators. That's what NSNumberFormatter is for. Particularly, if you hardcore the separator strings, your app will break in different locales.
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden