NSNumberFormatter 不接受小数点后的零

发布于 2024-12-06 08:48:49 字数 1250 浏览 1 评论 0原文

类似的问题也有人提出过,但我发现所提供的解决方案不适用于最新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈甜 2024-12-13 08:48:49

为什么要替换“.”和 ”。”?实例化数字后,您在调试器中看到什么?

我从未使用过 NSDecimalNumber,但前段时间已经在双打方面遇到了困难。我建议分解这个问题。使用硬编码值实例化 number,对其进行格式化并将其写入日志文件。如果这不起作用,请尝试下面的代码,它在我的应用程序中使用双值运行良好。我正在使用德语区域设置,但这不是重点。

NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
[formatter setAllowsFloats:TRUE];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMinimumSignificantDigits:20];
[formatter setMaximumSignificantDigits:20];
[formatter setMinimumFractionDigits:0];
[formatter setMaximumFractionDigits:5];
NSLocale* locale = [NSLocale currentLocale];
[formatter setLocale:locale];
NSNumber* number = [NSNumber numberWithDouble:1000.0123f];
myTextField.text = [formatter stringFromNumber:number];

如果您的代码已经可以工作,

  • 请检查是否没有 O 而不是零
  • 删除此“。”上面提到的替换
  • 我发现设置语言环境明确解决了一些问题(很久以前就忘记了细节,但你可以尝试一下)
  • 用 NSNumber 替换 NSDecimalNumber 并用双精度值提供它
  • 检查您的 XIB 文件并确保您的文本字段不执行任何其他格式化

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.

NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
[formatter setAllowsFloats:TRUE];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMinimumSignificantDigits:20];
[formatter setMaximumSignificantDigits:20];
[formatter setMinimumFractionDigits:0];
[formatter setMaximumFractionDigits:5];
NSLocale* locale = [NSLocale currentLocale];
[formatter setLocale:locale];
NSNumber* number = [NSNumber numberWithDouble:1000.0123f];
myTextField.text = [formatter stringFromNumber:number];

If your code does work already,

  • check that there is no O instead of zero
  • Remove this "." replacement mentioned above
  • I found out that setting a locale explicitly solved some problems (it's too long ago to remember details but you might give it a try)
  • Replace NSDecimalNumber by NSNumber and feed it with double values
  • Check your XIB file and ensure that your text field does not perform any other formatting
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文