为什么 NSNumberFormatter 不删除小数点和尾随零?
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还可以使用 setMaximumFractionDigits 截断分数:
其结果是:
这适用于 iOS 2.0 及更高版本。
You can also use setMaximumFractionDigits to truncate the fraction:
Which results in:
This works in iOS 2.0 and later.
可能有点晚了,你问题的核心已经得到很好的回答。但我想提醒您避免强制使用分隔符和小数点行为。建议尊重设备的区域设置,以显示具有正确区域设置行为的数字。
1. NSNumberFormatter:
2.显示具有正确区域设置的数字:
在您的情况下,可能实际上没有必要使用
NSNumberFormatter
,因为设备无论如何都会显示分隔符。为了从原始数据类型(例如int
)构建具有正确区域设置行为的NSString
,只需使用localizedStringWithFormat
并将数字格式化如下:具有
NSNumber
对象:3。使用
NSNumberFormatter
显示数字(正确的区域设置):在标签或文本字段中使用 NSNumber 对象
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:
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 aNSString
from primitive data types (e.g.int
) with the right locale behavior just uselocalizedStringWithFormat
and format the number as follows:with having an
NSNumber
objects:3. show numbers with
NSNumberFormatter
(right locale):Using the NSNumber object
longNumber
in labels or textFields e.g.: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.
这将删除小数点和尾随零。
和/或
This will remove the decimal point and trailing zeros.
and/or
这将解决它!
输出:
This will fix it!
Output: