Limiting both the fractional and total number of digits when formatting a float for display

时间秒杀一切 提交于 2019-12-01 17:01:31

You're looking for a combination of "maximum significant digits" and "maximum fraction digits", along with particular rounding behavior. NSNumberFormatter is equal to the task:

float twofortythreetwentyfive = 234.25;
float onetwothreefourtwentyfive = 1234.25;
float eleventwothreefourtwentyfive = 11234.25;

NSNumberFormatter * formatter =  [[NSNumberFormatter alloc] init];
[formatter setUsesSignificantDigits:YES];
[formatter setMaximumSignificantDigits:5];
[formatter setMaximumFractionDigits:2];
[formatter setRoundingMode:NSNumberFormatterRoundCeiling];

NSLog(@"%@", [formatter stringFromNumber:[NSNumber numberWithFloat:twofortythreetwentyfive]]);
NSLog(@"%@", [formatter stringFromNumber:[NSNumber numberWithFloat:onetwothreefourtwentyfive]]);
NSLog(@"%@", [formatter stringFromNumber:[NSNumber numberWithFloat:eleventwothreefourtwentyfive]]);

Result:

2012-04-26 16:32:04.481 SignificantDigits[11565:707] 234.25
2012-04-26 16:32:04.482 SignificantDigits[11565:707] 1234.3
2012-04-26 16:32:04.483 SignificantDigits[11565:707] 11235

Code :

#define INTPARTSTR(X) [NSString stringWithFormat:@"%d",(int)X]
#define DECPARTSTR(X) [NSString stringWithFormat:@"%d",(int)(((float)X-(int)X)*100)]

- (NSString*)formatFloat:(float)f
{
    NSString* result;

    result = [NSString stringWithFormat:@"%.2f",f];

    if ([DECPARTSTR(f) isEqualToString:@"0"]) return INTPARTSTR(f);
    if ([INTPARTSTR(f) length]==5) return INTPARTSTR(f);

    if ([result length]>5)
    {
        int diff = (int)[result length]-7;

        NSString* newResult = @"";

        for (int i=0; i<[result length]-diff-1; i++)
            newResult = [newResult stringByAppendingFormat:@"%c",[result characterAtIndex:i]];

        return newResult;
    }


    return result;
}

Testing it :

- (void)awakeFromNib
{
    NSLog(@"%@",[self formatFloat:234.63]);
    NSLog(@"%@",[self formatFloat:1234.65]);
    NSLog(@"%@",[self formatFloat:11234.65]);
    NSLog(@"%@",[self formatFloat:11234]);
}

Output :

2012-04-26 19:27:24.429 newProj[1798:903] 234.63
2012-04-26 19:27:24.432 newProj[1798:903] 1234.6
2012-04-26 19:27:24.432 newProj[1798:903] 11234
2012-04-26 19:27:24.432 newProj[1798:903] 11234

Here is how I implemented this in my code. I don't know how efficient it is, I hope not bad.

So I create a global NSNumberFormatter

NSNumberFormatter* numFormatter; 

and initialize it somewhere:

numFormatter=[[NSNumberFormatter alloc]init];

Then I format number with the following function:

- (NSString*)formatFloat:(Float32)number withOptimalDigits:(UInt8)optimalDigits maxDecimals:(UInt8)maxDecimals
{
    NSString* result;
    UInt8 intDigits=(int)log10f(number)+1;
    NSLog(@"Formatting %.5f with maxDig: %d maxDec: %d intLength: %d",number,optimalDigits,maxDecimals,intDigits);  

    numFormatter.maximumFractionDigits=maxDecimals;
    if(intDigits>=optimalDigitis-maxDecimals) {
        numFormatter.usesSignificantDigits=YES;
        numFormatter.maximumSignificantDigits=(intDigits>optimalDigits)?intDigits:optimalDigits;
    } else {
        numFormatter.usesSignificantDigits=NO;
    }
    result = [numFormatter stringFromNumber:[NSNumber numberWithFloat:number]];

    return result;
}
PokerIncome.com

Is this a bug when using maximumFractionDigits and maximumSignificantDigits together on NSNumberForamtter on iOS 8?

        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.maximumFractionDigits = 2;  
        formatter.maximumSignificantDigits = 3;
        NSLog(@"%@", [formatter stringFromNumber:@(0.3333)]); // output 0.333 expected 0.33

It works fine if I only use maximumFractionDigits

        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.maximumFractionDigits = 2;
        NSLog(@"%@", [formatter stringFromNumber:@(0.3333)]); // output expected .33

NSNumberFormatter maximumFractionDigits and maximumSignificantDigits bug

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