NSNumberFormatter 不接受小数点后的零
类似的问题也有人提出过,但我发现所提供的解决方案不适用于最新的 iOS SDK,或者不完全适用于这种特殊情况。
我使用 NSNotification 调用一个方法,该方法使用 UILabel 中的分组符号来格式化数字,因为它是使用按钮输入中的数字输入的。它也适用于整数和小数——只要小数点后不输入零。例如,如果输入 2000,则显示为 2,000,因为输入得很好。如果输入 23.345,也显示得很好。对这些数字所做的任何计算都是正确的。但不可能输入0.0001或0.2340007!它只是不会在小数点后添加任何零。
我从 stackoverflow 上的另一篇文章中得到了原始的代码想法:
NSString *currentText = [display_ text];
currentText = [currentText stringByReplacingOccurrencesOfString:@","
withString:@""];
currentText = [currentText stringByReplacingOccurrencesOfString:@"."
withString:@"."];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setGroupingSeparator:@","];
[formatter setDecimalSeparator:@"."];
[formatter setMaximumFractionDigits:10];
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:currentText];
NSString *finalText = [formatter stringFromNumber:number];
NSString *lastChar = [currentText substringFromIndex:[currentText length] - 1];
if ([lastChar isEqualToString:@"."])
{
finalText = [finalText stringByAppendingString:@"."];
}
[display_ setText: finalText];
[formatter release];
格式化程序的参数的任何欺骗(我已经尝试过)都不会允许在小数点后输入零。
Similar questions have been asked but I find that the offered solutions don't work with the latest iOS SDK or are not exactly applicable in this particular case.
I call a method using an NSNotification, that formats a number with grouping symbols in a UILabel as it is entered using digits from button input. It works great for integers and decimals too--so long as no zeros are entered after a decimal point. For example if 2000 is entered, it is displayed as 2,000 as it is entered just fine. If 23.345 is entered, that displays just fine too. Any calculations done on these numbers come out correct. But it is impossible to enter 0.0001 or 0.2340007! It just won't take and append any zeros after the decimal point.
I got the original code idea from another post here at stackoverflow:
NSString *currentText = [display_ text];
currentText = [currentText stringByReplacingOccurrencesOfString:@","
withString:@""];
currentText = [currentText stringByReplacingOccurrencesOfString:@"."
withString:@"."];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setGroupingSeparator:@","];
[formatter setDecimalSeparator:@"."];
[formatter setMaximumFractionDigits:10];
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:currentText];
NSString *finalText = [formatter stringFromNumber:number];
NSString *lastChar = [currentText substringFromIndex:[currentText length] - 1];
if ([lastChar isEqualToString:@"."])
{
finalText = [finalText stringByAppendingString:@"."];
}
[display_ setText: finalText];
[formatter release];
No amount of finagling (that I have tried) with the parameters of the formatter will allow the entry of zeros after the decimal point.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要替换“.”和 ”。”?实例化数字后,您在调试器中看到什么?
我从未使用过 NSDecimalNumber,但前段时间已经在双打方面遇到了困难。我建议分解这个问题。使用硬编码值实例化
number
,对其进行格式化并将其写入日志文件。如果这不起作用,请尝试下面的代码,它在我的应用程序中使用双值运行良好。我正在使用德语区域设置,但这不是重点。如果您的代码已经可以工作,
Why do you replace "." with "."? What do you see in debugger after number is instantiated?
I have never worked with NSDecimalNumber but struggled already with doubles some time ago. I would suggest breaking down the problem. Instantiate
number
with a hard-coded value, format it and write it to log file. If this doesn't work try my code below, it works fine in my app with double values. I am using German locale settings but that shouldn't be the point.If your code does work already,