I have a text field for the user to enter a number. Specifically, an integer. So I made a nice little action and hooked the text field up to call it when done editing. It looks like this:
Simple right? It turns the text into an integer, and sets the text field to show that integer. It works great: you enter 6, it shows 6. You enter abc, it shows 0. You enter 3.14159, it shows 3.
When you enter a number with 4+ digits, so anything 1000 and up, it adds commas. So if you type 1234567, it shows 1,234,567. I like that feature a lot. It makes it easy to read.
However once the text field has the commas, for example 1,000, then when you click away or hit enter, it loses everything from the first comma on. So 4,096 turns into 4. Kind of a problem.
I know I could use a number formatter to do what I want, but that is kind of overkill for such a simple task. I could also get the string value from the text field and remove all commas first but that seems like a hassle.
So I guess my question is, why is there an asymmetry in the NSTextField class, whereby integerValue chokes on commas but setIntegerValue inserts them?
Code:
- (IBAction)editNumber:(id)sender {
[sender setIntegerValue:[sender integerValue]];
}
Simple right? It turns the text into an integer, and sets the text field to show that integer. It works great: you enter 6, it shows 6. You enter abc, it shows 0. You enter 3.14159, it shows 3.
When you enter a number with 4+ digits, so anything 1000 and up, it adds commas. So if you type 1234567, it shows 1,234,567. I like that feature a lot. It makes it easy to read.
However once the text field has the commas, for example 1,000, then when you click away or hit enter, it loses everything from the first comma on. So 4,096 turns into 4. Kind of a problem.
I know I could use a number formatter to do what I want, but that is kind of overkill for such a simple task. I could also get the string value from the text field and remove all commas first but that seems like a hassle.
So I guess my question is, why is there an asymmetry in the NSTextField class, whereby integerValue chokes on commas but setIntegerValue inserts them?