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: Philip McIntosh <email@hidden>
- Date: Wed, 28 Sep 2011 16:50:43 -0600
> Message: 11
> Date: Wed, 28 Sep 2011 09:28:42 -0700
> From: Kyle Sluder <email@hidden>
> Subject: Re: Numeric Entry and Formatting With NSNumberFormatter Won't
> Append Zeros
> To: Philip McIntosh <email@hidden>
> Cc: "email@hidden" <email@hidden>
> Message-ID: <email@hidden>
> Content-Type: text/plain; charset=us-ascii
>
> 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
Yes and I want the grouping symbols and decimal separator to show up as the string is built up.
>
> Return key pressed: UITextView string --(NSNumberFormatter)--> NSDecimalNumber stored in the model layer
That is exactly what happens.
>
> 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.
No, I want the number in the display (which is a string representation of the number) to be formatted as it is entered not after any "return" or "calculate" keys are pressed. I can get it to format and display the string correctly "after" such a key is pressed, no problem.
>
> All computations should be performed on the stored NSDecimalNumber, not by constantly reinterpreting the string value of the text view.
Sure. The computations are not initiated until the string is finished and the "next" button is tapped.
>
> 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.
>
Regarding the decimalNumberWithString method, the documentation states: "Besides digits, numericString can include an initial “+” or “–”; a single “E” or “e”, to indicate the exponent of a number in scientific notation; and a single NSDecimalSeparator to divide the fractional from the integral part of the number." There is no indication that an NSDecimalNumber is expecting any grouping separators--which is why I strip them out.
Be assured that all my computations are certainly done after assigning the inputs to NSDecimalNumbers. In fact my app works perfectly. All I was trying to do was make it a little more user-friendly by letting the user see the number grouping separators being displayed as a number was entered instead of having to wait until a "next" button was tapped, at which time my formatter formatted it perfectly. The issue is that I have thus far been able to come up with a method for doing that that works for both integers and decimals. As I said in my initial post, I can't get it to display any zeros after a decimal point. What is maddening is that it is only zeros that it will not accept, any other digit works fine! It is a "enter and display a number" issue, not a "use a number" issue.
The locale issue is one I am not exactly clear on. If I instantiate a formatter with explicit settings for what grouping and separator symbols I want used, won't that override any locale settings that the OS would attempt to enforce by default? Here's why I want to do that. Let's say the user is from , Oh I don't know--Albania--and has been looking at numbers formatted like 23.456.781,30042 all her life. Shouldn't she be given the option of continuing to look at numbers that way no matter where in the world she is and no matter what locale her device is set to? I think so.
_______________________________________________
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