NSNumberFormatter displays 1,000,000 as 1 million. Etc.

As described in the header, is there a way to format a huge amount, for example, 1,000,000 (1 million) or 45,500,000 (45.5 million) lines to display an abbreviated version of this name. I just want to prevent all the tips to do it manually. I know how to do it. I just want to find out if there is an easier way to use NSNumberFormatter.

Greetings

Lukash

+3
source share
3 answers

NSNumberFormatter. NSNumberFormatter. , , > 1,000,000, , - "" . , .

+6

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
+4

No, I don’t think there is a way to do this using NSNumberFormatter. You're on your own.

+1
source

All Articles