I'm using NSNumberFormatter
to format float values as integer strings, i.e. omitting the fractional part. I find it odd, that numbers in range (-0.5, 0)
*open interval end up as -0
. As this value will be displayed to the user, I think that negative zero is not appropriate. I have experimented with various combinations of numberStyle
and roundingMode
without success.
有没有办法配置NSNumberFormatter
将它们输出为0
,或者我是否必须对该范围采取手动校正?
不,没有办法配置它来做到这一点。
在“10.4模式”下,NSNumberFormatter基本上只是包装CFNumberFormatter(尽管它不是直接的免费桥接包装)。您可以查看数字格式化程序属性键列表,很明显没有任何东西可以满足您的要求。 (在“10.0”模式下可能是可能的;需要一些尝试和错误才能找到答案。但我怀疑你想使用它。)
因此,预舍入(正如 Justin Boo 所建议的那样)可能是您的最佳选择。
当然,您可以进行后处理。您想要做什么可能取决于您是否也想将 -0.00 渲染为 0.00、您希望不使用“-0”的本地化发生什么,等等。最简单的情况如下:
@interface NSNumberFormatter (NegativeZero)
- (NSString *)stringFromNumberNoNegativeZero:(NSNumber *)number;
@end
@implementation NSNumberFormatter (NegativeZero)
- (NSString *)stringFromNumberNoNegativeZero:(NSNumber *)number {
NSString *s = [self stringFromNumber:number];
if ([s isEqualToString:@"-0"]) return @"0";
return s;
}
@end
但如果你想要更复杂的东西,它就会变得更复杂。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)