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: Thu, 29 Sep 2011 17:06:19 -0600
>
> I suspect you're halfway to the solution. When you get a partially-entered string, you are already looking for the decimal point (which should of course be a localized comparison). You *could* try to insert the grouping separators yourself, but that would potentially involve writing a formatter for every localized display format, so that's not a great approach. Alternately, you could take the substring *before* the decimal point and run that through a regular string/numeric/string conversion to get the group separators in place. (If there's a decimal point, you know that what's before it is a *complete* whole number.) Then re-combine that with the original post-decimal-point substring -- with the trailing "0" characters intact.
>
> Such an approach involves only two assumptions I can think of:
>
> 1. It's always possible to recognize a decimal point if present, and divide the string there, so that the initial portion is a well-formed (whole) number.
>
> 2. The rules for inserting grouping separators don't depend on what comes after the decimal point, if present.
>
> Those assumptions don't sound too bad to me.
>
I am now doing what I believe is essentially what you suggest. I test for a decimal point, and if there isn't one, I let the formatter do its thing and it applies the grouping separators just fine. If a decimal point is detected I no longer call the formatter and I let the string be appended after the decimal point with no further formatting. It's working so far.
> On Sep 28, 2011, at 4:26 PM, Quincey Morris wrote:
>
>> On Sep 28, 2011, at 15:50 , Philip McIntosh wrote:
>>
>>> 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.
>>
>> I just went back and read as much of this thread as I could find (all of it, I think) because I wasn't paying much attention earlier.
>>
>> It seems to me you've created your own problem. You cannot re-create a string that represents certain user-entered state [i.e. a partially-entered number] if you run the input through a transformation that discards a vital piece of the state. Converting a string to a number to a string [for a partially-entered number] preserves *most* of the state, but not all of it, because the numeric conversion you're using isn't intended for a partially-entered number.
>>
>> Specifically, if the user has typed "1.000" on the way to "1.0002", the number to be converted is in fact 1.0002 even if partially entered. Pretending temporarily that it's a fully-entered "1.000" throws away the part of the state that represents partial completion, so the result is of course "1.". That's what you asked for.
>>
>> By replacing the partially user-entered string before the user has finished entering it, you are already abusing the Cocoa text field metaphor to some degree.
I'm not sure if makes any difference to your "abusing the Cocoa text field metaphor to some degree" argument, but I am using a label not a textField.
>
> I suspect you're also going to confuse the undo manager. I've had issues in the past when mixing it with formatters because the formatter changed the value that the text field was using but undo didn't notice, and then tried to undo more or less characters than were actually entered.
I have not implemented any undo functionality, but thanks for the heads up. That will definitely have to be considered, since I was thinking about implementing undo at some point.
I have it working now. Thanks for everyone's interest and input. On to the next problem._______________________________________________
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