NSNumberFormatter rounding to negative zero

坚强是说给别人听的谎言 提交于 2019-12-04 05:43:58

I had to do correct this myself. I am using the NSNumberFormatter to display temperature with default numberStyleNSNumberFormatterNoStyle which rounds the numbers to the whole integer, roundingMode set to NSNumberFormatterRoundHalfUp. In the end I did intercept the values in problematic range and round it myself:

- (NSString *)temperature:(NSNumber *)temperature
{
    float f = [temperature floatValue];
    if (f < 0 && f > -0.5)
        temperature = [NSNumber numberWithLong:lround(f)];

    return [self.temperatureFormatter stringFromNumber:temperature];
}

No, there is no way to configure it to do that.

In "10.4 mode", NSNumberFormatter basically just wraps CFNumberFormatter (although it's not a direct toll-free-bridged wrapping). You can look at the list of Number Formatter Property Keys and it's pretty clear that there's nothing that will do what you want. (It may be possible in "10.0" mode; it would take a bit of trial and error to find out. But I doubt you want to use that.)

So pre-rounding (as Justin Boo suggests) is probably your best option.

You could, of course, post-process instead. Exactly what you want to do probably depends on whether you want to also render -0.00 as 0.00, what you want to happen for localizations that don't use "-0", etc. The simplest case would be as simple as this:

@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

But if you want anything more complicated, it'll get more complicated.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!