NSNumberFormatter 不允许小数输入
如果我输入小数点“.”在我的 UITextField 中,调用的数字格式化程序无法识别小数点并继续,就像尚未输入小数点一样。即,如果我输入 200.9,小数点将不会显示在文本字段中,文本字段的文本将为 2009。
我想将小数点后的位数限制为 2,正如我相信我在下面所做的那样。请问你能告诉我我做了什么导致了这个吗?
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
BOOL isDeleting = [textField.text substringWithRange:range].length > string.length;
int index = [textFields indexOfObject:textField];
NSString *input;
if (isDeleting == NO)
input = [textField.text stringByAppendingString:string];
else {
NSMutableString *str = [textField.text mutableCopy];
[str deleteCharactersInRange:range];
input = [[str copy] autorelease];
[str release];
}
if ([input isEqualToString:@"£"] || ([input isEqualToString:@""] && index != 1)) {
[textField setText:@"£"];
}
else {
if (index != 1)
[textField setText:[self numberFormattedString:input]];
else
[textField setText:input];
}
return NO;
}
- (NSString *) numberFormattedString:(NSString *)str {
str = [str stringByReplacingOccurrencesOfString:@"£" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"," withString:@""];
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-UK"];
[formatter setLocale:locale];
[locale release];
[formatter setAllowsFloats:YES];
[formatter setMaximumFractionDigits:3];
[formatter setMinimumFractionDigits:0];
[formatter setDecimalSeparator:@"."];
return [formatter stringFromNumber:[NSNumber numberWithFloat:[str floatValue]]];
}
TIA。
If I enter a decimal point '.' into my UITextField, the number formatter called does not recognise the decimal point and continues as if the decimal point has not been entered. I.e If I entered 200.9, the decimal point would not show up in the textfield and the text of the textfield would be 2009.
I want to limit the number of digits after the decimal point to 2 as I believe I am doing below. Please can you tell me what I am doing to cause this?
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
BOOL isDeleting = [textField.text substringWithRange:range].length > string.length;
int index = [textFields indexOfObject:textField];
NSString *input;
if (isDeleting == NO)
input = [textField.text stringByAppendingString:string];
else {
NSMutableString *str = [textField.text mutableCopy];
[str deleteCharactersInRange:range];
input = [[str copy] autorelease];
[str release];
}
if ([input isEqualToString:@"£"] || ([input isEqualToString:@""] && index != 1)) {
[textField setText:@"£"];
}
else {
if (index != 1)
[textField setText:[self numberFormattedString:input]];
else
[textField setText:input];
}
return NO;
}
- (NSString *) numberFormattedString:(NSString *)str {
str = [str stringByReplacingOccurrencesOfString:@"£" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"," withString:@""];
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-UK"];
[formatter setLocale:locale];
[locale release];
[formatter setAllowsFloats:YES];
[formatter setMaximumFractionDigits:3];
[formatter setMinimumFractionDigits:0];
[formatter setDecimalSeparator:@"."];
return [formatter stringFromNumber:[NSNumber numberWithFloat:[str floatValue]]];
}
TIA.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您输入数字 100,然后输入小数点。当使用字符串
£100.
调用 numberFormattedString 时,它将删除 £,并且 str 将包含字符串100.
,其中[str floatValue]
转换为浮点值 100,最后您的数字格式化程序将其以字符串£100
的形式返回,不带任何小数点。所以基本上[str floatValue]
正在杀死你的小数点。一种解决方案是检查何时在文本字段委托方法中获取小数点作为替换字符串,并在这种情况下跳过调用 numberFormattedString。然后,当用户输入下一个数字时,您可以继续调用 numberFormattedString,并且转换应该正确发生。您只需确保用户只能输入一位小数点。
编辑:我刚刚意识到,如果您在小数点后输入 0(例如 100.0),我建议的解决方案仍然不起作用,但我确信您可以通过一些小的调整来解决这个问题。
Let's say you enter the number 100 and then a decimal point. When numberFormattedString is called with the string
£100.
, it's going to get rid of the £, and str will contain the string100.
which[str floatValue]
converts to the float value 100, and finally your number formatter spits it back out as the string£100
without any decimal point. So basically[str floatValue]
is killing your decimal point.One solution is to check for when you get the decimal point as your replacement string in the text field delegate method, and skip calling numberFormattedString in that case. Then when the user enters the next digit, you can carry on calling numberFormattedString, and the conversion should happen correctly. You'll just have to make sure that the user can only enter one decimal point.
EDIT: I just realized that my suggested solution still won't work if you enter a 0 after the decimal point (e.g. 100.0), but I'm sure there is a minor tweak that you can figure out to solve that.