Disable Regional settings for NSTextField
I have a problem with NSTextField in Cocoa. Is there a way to remove regional settings from a NSTextField
I want the user to only enter values like so 0.6, 0.7 but if the u开发者_如何学JAVAser is not from US he is forced to enter like this 0,6, 0,7
So if the user is not US user if he enters 0.7 [textField floatValue] will return 0 and if the user is US and he enters 0,7 [textField floatValue] will return 0
Well lets say that is something i can accept but then i want to format the value and display it only with 3 decimals
this will work for US but will not work for non US even though value is a correct float (eg 0.3) [textField setStringValue:[NSString stringWithFormat:@"%.3f",value]];
textfield is of type NSTextField
Thank you!
You should be using NSNumberFormatter
instead of +[NSString stringWithFormat:]
. In fact, using NSNumberFormatter
allows you to:
- Configure the minimum and maximum number of digits after the decimal separator;
- Configure the string that’s used as the decimal separator in case you want to override the user’s locale;
- Configure the rounding behaviour, if that’s important;
amongst other features.
If your NSTextField
is in a nib file, Interface Builder/Xcode 4 allows you to link the formatter
outlet to an NSNumberFormatter
instance. You can also do it programatically by sending -setFormatter:
to the text field.
精彩评论