为什么 NSNumberFormatter 不删除小数点和尾随零?

发布于 2024-10-28 01:22:37 字数 829 浏览 1 评论 0原文

我想将 unsigned long int 转换为 NSString,格式为每 3 位逗号,既没有小数点,也没有尾随零。我可以使用 NSNumberFormatter 将逗号放入 NSString 中,没有任何问题,但由于某种原因,我无法让小数点和零消失。

这是我的代码:

NSNumberFormatter *fmtr;
unsigned long int intToBeDisplayed;
intToBeDisplayed = 1234567890;
fmtr = [[[NSNumberFormatter alloc] init] autorelease];
[fmtr setUsesGroupingSeparator:YES];
[fmtr setGroupingSeparator:@","];
[fmtr setGroupingSize:3];
[fmtr setAlwaysShowsDecimalSeparator:NO];
NSLog([fmtr stringFromNumber:[NSNumber numberWithUnsignedLong:intToBeDisplayed]]);

日志显示“1,234,567,890.00”,适当添加逗号,但不幸的是不需要的“.00”。我已经尝试使用 int 而不是 unsigned long int 来构造这个构造,并使用较小的整数值,并且来自各个博客的其他构造都无济于事。我将非常感谢任何有关如何从 NSString 中获取小数点和尾随零的建议。

我是 Objective-C 的新手,在尝试解决这个问题时遇到了眼花综合症,但在这个阶段,在 Objective-C 中学到的每一个小教训和取得的成功都是巨大快乐的源泉。

I want to convert an unsigned long int to an NSString formatted with commas every 3 digits and neither a decimal point nor trailing zeros. I can get the commas into the NSString without any trouble using NSNumberFormatter, but for some reason I can't get the decimal point and zeros to go away.

This is my code:

NSNumberFormatter *fmtr;
unsigned long int intToBeDisplayed;
intToBeDisplayed = 1234567890;
fmtr = [[[NSNumberFormatter alloc] init] autorelease];
[fmtr setUsesGroupingSeparator:YES];
[fmtr setGroupingSeparator:@","];
[fmtr setGroupingSize:3];
[fmtr setAlwaysShowsDecimalSeparator:NO];
NSLog([fmtr stringFromNumber:[NSNumber numberWithUnsignedLong:intToBeDisplayed]]);

The log displays "1,234,567,890.00", appropriately adding the commas but also unfortunately the unwanted ".00". I've tried this construct with int instead of unsigned long int, with smaller integer values, and other constructs from various blogs all to no avail. I'd be very grateful for any suggestions on how to get the decimal point and trailing zeros out of the NSString.

I'm a newbie to Objective-C, becoming on oldbie with respect to the blurry-eyed syndrome trying to solve this problem, but am at that stage where every little lesson learned and success achieved in Objective-C is a source of great joy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

酒解孤独 2024-11-04 01:22:37

您还可以使用 setMaximumFractionDigits 截断分数:

unsigned long int intToBeDisplayed = 1234567890;
NSNumber *longNumber = [NSNumber numberWithUnsignedLong:intToBeDisplayed];

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setUsesGroupingSeparator:YES];
[formatter setGroupingSeparator:@","];
[formatter setGroupingSize:3];
[formatter setMaximumFractionDigits:0];

NSString *formattedNumber = [formatter stringFromNumber:longNumber];
NSLog(@"The formatted number is %@", formattedNumber);

其结果是:

格式化后的数字为 1,234,567,890

这适用于 iOS 2.0 及更高版本。

You can also use setMaximumFractionDigits to truncate the fraction:

unsigned long int intToBeDisplayed = 1234567890;
NSNumber *longNumber = [NSNumber numberWithUnsignedLong:intToBeDisplayed];

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setUsesGroupingSeparator:YES];
[formatter setGroupingSeparator:@","];
[formatter setGroupingSize:3];
[formatter setMaximumFractionDigits:0];

NSString *formattedNumber = [formatter stringFromNumber:longNumber];
NSLog(@"The formatted number is %@", formattedNumber);

Which results in:

The formatted number is 1,234,567,890

This works in iOS 2.0 and later.

春花秋月 2024-11-04 01:22:37

可能有点晚了,你问题的核心已经得到很好的回答。但我想提醒您避免强制使用分隔符和小数点行为。建议尊重设备的区域设置,以显示具有正确区域设置行为的数字。

1. NSNumberFormatter:

NSNumberFormatter *valueFormatter = [[NSNumberFormatter alloc]init];
valueFormatter.locale = [NSLocale currentLocale];    // <-- allow the device locale setting
// valueFormatter.GroupingSeparator:@","];           // don't force locale behavior
valueFormatter.numberStyle = NSNumberFormatterDecimalStyle;
valueFormatter.usesGroupingSeparator = YES;
valueFormatter.groupingSize = 3;
valueFormatter.maximumFractionDigits = 0;            // this is important to have your primary issue solved

2.显示具有正确区域设置的数字:

在您的情况下,可能实际上没有必要使用NSNumberFormatter,因为设备无论如何都会显示分隔符。为了从原始数据类型(例如 int)构建具有正确区域设置行为的 NSString,只需使用 localizedStringWithFormat 并将数字格式化如下:

NSString *testString = [NSString localizedStringWithFormat:@"%lu", intToBeDisplayed];

具有 NSNumber 对象:

NSNumber *longNumber = [NSNumber numberWithUnsignedLong:intToBeDisplayed];
NSString *testString = [NSString localizedStringWithFormat:@"%lu", longNumber.unsignedLongValue];

3。使用 NSNumberFormatter 显示数字(正确的区域设置):

NSNumber *longNumber = [NSNumber numberWithUnsignedLong:intToBeDisplayed];
NSString *testString = [NSString localizedStringWithFormat:@"%@", [valueFormatter stringForObjectValue:longNumber]];

在标签或文本字段中使用 NSNumber 对象 longNumber 例如:

testLabel.text = [NSString localizedStringWithFormat:@"%@", [valueFormatter stringForObjectValue:longNumber]];

将在具有英语区域设置的设备上产生以下输出:

1,234,567,890

在具有(例如)德语区域设置的设备上,它将自动显示如下数字:

1.234.567.890

希望我可以为(困难的)数字格式问题添加更多内容。

It might be a little late and the core of your question is answered fine. But I want to bring to mind to avoid forcing the separator and decimal point behavior. It is rather recommended to respect the locale setting of the device to show numbers with the right locale behavior.

1. the NSNumberFormatter:

NSNumberFormatter *valueFormatter = [[NSNumberFormatter alloc]init];
valueFormatter.locale = [NSLocale currentLocale];    // <-- allow the device locale setting
// valueFormatter.GroupingSeparator:@","];           // don't force locale behavior
valueFormatter.numberStyle = NSNumberFormatterDecimalStyle;
valueFormatter.usesGroupingSeparator = YES;
valueFormatter.groupingSize = 3;
valueFormatter.maximumFractionDigits = 0;            // this is important to have your primary issue solved

2. show numbers with right locale:

In your case it might not really be necessary to use a NSNumberFormatter since the device will show the separator anyway. In order to built a NSString from primitive data types (e.g. int) with the right locale behavior just use localizedStringWithFormat and format the number as follows:

NSString *testString = [NSString localizedStringWithFormat:@"%lu", intToBeDisplayed];

with having an NSNumber objects:

NSNumber *longNumber = [NSNumber numberWithUnsignedLong:intToBeDisplayed];
NSString *testString = [NSString localizedStringWithFormat:@"%lu", longNumber.unsignedLongValue];

3. show numbers with NSNumberFormatter (right locale):

NSNumber *longNumber = [NSNumber numberWithUnsignedLong:intToBeDisplayed];
NSString *testString = [NSString localizedStringWithFormat:@"%@", [valueFormatter stringForObjectValue:longNumber]];

Using the NSNumber object longNumber in labels or textFields e.g.:

testLabel.text = [NSString localizedStringWithFormat:@"%@", [valueFormatter stringForObjectValue:longNumber]];

would produce the following output on devices with english locale setting:

1,234,567,890

on devices with (e.g.) german locale setting it would automatically show the number as follows:

1.234.567.890

Hope I could add a little bit more to the (difficult) number formatting issue.

百变从容 2024-11-04 01:22:37

这将删除小数点和尾随零。

[fmtr setPositiveFormat:@"#"]

和/或

[fmtr setNegativeFormat:@"#"]

This will remove the decimal point and trailing zeros.

[fmtr setPositiveFormat:@"#"]

and/or

[fmtr setNegativeFormat:@"#"]
霊感 2024-11-04 01:22:37

这将解决它!

 [fmtr setGeneratesDecimalNumbers:NO];

输出:

2011-03-31 18:40:45.645 Stephen[41552:903] 1,234,567,890

This will fix it!

 [fmtr setGeneratesDecimalNumbers:NO];

Output:

2011-03-31 18:40:45.645 Stephen[41552:903] 1,234,567,890
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文