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 mean, you're interfering with the user, who might be confused by not 
seeing what was actually typed, based on a belief that the difference is small 
enough to be either really confusing or annoying.) Discarding state just adds 
the last straw that makes things break down.

So what you need is a parser that decodes partially-entered numeric strings, 
not a parser that translates numeric strings into numbers.

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.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to