就像标题中所述,是否有任何方法可以将1,000,000(1,500n)或45,500,000(45,5 mln)等巨大数字格式化为字符串以显示此数字名称的缩写版本。我只是想阻止所有的建议手动。我知道如何做到这一点。我只是想知道是否有更简单的方法使用NSNumberFormatter。NSNumberFormatter将1,000,000显示为1mln等
干杯,
卢卡斯
就像标题中所述,是否有任何方法可以将1,000,000(1,500n)或45,500,000(45,5 mln)等巨大数字格式化为字符串以显示此数字名称的缩写版本。我只是想阻止所有的建议手动。我知道如何做到这一点。我只是想知道是否有更简单的方法使用NSNumberFormatter。NSNumberFormatter将1,000,000显示为1mln等
干杯,
卢卡斯
我会建议手动和使用NSNumberFormatter的组合。我的想法是子类NSNumberFormatter。如果要格式化的数字大于1,000,000,则可以对其进行分割,使用超级实现格式化结果,并在末尾附加“mln”。只做你不能为你做的部分。
不,我不认为有一种方式与NSNumberFormatter做到这一点。你对此自行决定。
这里是一个NSNumberFormatter子类,做它的草图(对不起,格式稍微偏离):
@implementation LTNumberFormatter
@synthesize abbreviationForThousands;
@synthesize abbreviationForMillions;
@synthesize abbreviationForBillions;
-(NSString*)stringFromNumber:(NSNumber*)number
{
if (! (abbreviationForThousands || abbreviationForMillions || abbreviationForBillions))
{
return [super stringFromNumber:number];
}
double d = [number doubleValue];
if (abbreviationForBillions && d > 1000000000)
{
return [NSString stringWithFormat:@"%@ %@", [super stringFromNumber:[NSNumber numberWithDouble:d/1000000000]], abbreviationForBillions];
}
if (abbreviationForMillions && d > 1000000)
{
return [NSString stringWithFormat:@"%@ %@", [super stringFromNumber:[NSNumber numberWithDouble:d/1000000]], abbreviationForMillions];
}
if (abbreviationForThousands && d > 1000)
{
return [NSString stringWithFormat:@"%@ %@", [super stringFromNumber:[NSNumber numberWithDouble:d/1000]], abbreviationForThousands];
}
return [super stringFromNumber:number];
}
@end