我需要最有效地在有限宽度的区域打印浮点值。我正在使用NSNumberFormatter
,我将小数点后的两个数字设置为默认值,以便当我有一个像234.25
这样的数字时,它会按原样打印:234.25
.但是当我有1234.25
我希望它被打印为:1234.3
,11234.25
应该打印11234
。
两位数,如果我在点后有数字,则总共最多需要五位数,但如果整数部分有更多数字,它也应该打印五位以上。
我没有看到限制NSNumberFormatter
总数的能力.这是否意味着我应该编写自己的函数来以这种方式格式化数字?如果是这样,那么获取数字的整数和小数部分的位数的正确方法是什么?我也更喜欢使用 CGFLoat
,而不是NSNumber
以避免额外的类型转换。
您正在寻找"最大有效数字"和"最大分数数字"的组合,以及特定的舍入行为。 NSNumberFormatter
等于任务:
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]]);
结果:
2012-04-2616:32:04.481 有效数字[11565:707] 234.25
2012-04-26 16:32:04.482 有效数字[11565:707] 1234.3
2012-04-26 16:32:04.483 有效数字[11565:707] 11235
代码 :
#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;
}
测试它:
- (void)awakeFromNib
{
NSLog(@"%@",[self formatFloat:234.63]);
NSLog(@"%@",[self formatFloat:1234.65]);
NSLog(@"%@",[self formatFloat:11234.65]);
NSLog(@"%@",[self formatFloat:11234]);
}
输出:
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
以下是我在代码中实现这一点的方式。我不知道它的效率如何,我希望还不错。
所以我创建了一个全局 NSNumberFormatter
NSNumberFormatter* numFormatter;
并在某处初始化它:
numFormatter=[[NSNumberFormatter alloc]init];
然后我使用以下函数格式化数字:
- (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;
}
iOS 8上一起使用maximumFractionDigits
和maximumSignificantDigits
一起使用NSNumberForamtter
时,这是一个错误吗?
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.maximumFractionDigits = 2;
formatter.maximumSignificantDigits = 3;
NSLog(@"%@", [formatter stringFromNumber:@(0.3333)]); // output 0.333 expected 0.33
如果我只使用maximumFractionDigits
它工作正常
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.maximumFractionDigits = 2;
NSLog(@"%@", [formatter stringFromNumber:@(0.3333)]); // output expected .33
NSNumberFormatter maximumFractionDigits 和 MaximumImportantDigits bug