NSNumberFormatter rounding to negative zero

2020-07-11 05:04发布

问题:

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.

Is there a way to configure the NSNumberFormatter to output them as 0, or do I have to resort to manual correction for that range?

回答1:

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];
}


回答2:

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.