Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert int to shortened, formatted string

The user will enter a dollar value as an int, and I'd like to convert the result into a shortened, formatted string. So if the user enters 1700, the string would say "$1.7k". If the user enters 32600000, the string would say "$32.6m".

Update

Here's the code I have so far. It seems to be working for numbers ~10k. I would just add more if statements for bigger numbers. But is there a more efficient way to do this?

NSNumberFormatter *nformat = [[NSNumberFormatter alloc] init]; 
[nformat setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
[nformat setCurrencySymbol:@"$"]; 
[nformat setNumberStyle:NSNumberFormatterCurrencyStyle]; 
double doubleValue = 10200; 
NSString *stringValue = nil; 
NSArray *abbrevations = [NSArray arrayWithObjects:@"k", @"m", @"b", @"t", nil] ; 

for (NSString *s in abbrevations) 
{ 

    doubleValue /= 1000.0 ; 

    if ( doubleValue < 1000.0 ) 
    { 

        if ( (long long)doubleValue % (long long) 100 == 0 ) { 
            [nformat setMaximumFractionDigits:0]; 
        } else {                 
            [nformat setMaximumFractionDigits:2]; 
        } 

        stringValue = [NSString stringWithFormat: @"%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]] ]; 
        NSUInteger stringLen = [stringValue length]; 

        if ( [stringValue hasSuffix:@".00"] ) 
        {                
            // Remove suffix 
            stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-3)];             
        } else if ( [stringValue hasSuffix:@".0"] ) { 

            // Remove suffix 
            stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-2)]; 

        } else if ( [stringValue hasSuffix:@"0"] ) { 

            // Remove suffix 
            stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-1)];         
        } 


        // Add the letter suffix at the end of it 
        stringValue = [stringValue stringByAppendingString: s]; 

        //stringValue = [NSString stringWithFormat: @"%@%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]]  , s] ; 
        break ; 
    }    
}  

NSLog(@"Cash = %@", stringValue); 
like image 761
bmueller Avatar asked Aug 16 '12 19:08

bmueller


2 Answers

unsigned long long value = 1700llu;
//value = 32600001llu;
//value = UINT64_MAX;

NSUInteger index = 0;
double dvalue = (double)value;
//Updated to use correct SI Symbol ( http://en.wikipedia.org/wiki/SI_prefix )
NSArray *suffix = @[ @"", @"k", @"M", @"G", @"T", @"P", @"E" ];

while ((value/=1000) && ++index) dvalue /= 1000;

NSString *svalue = [NSString stringWithFormat:@"$%.*f%@",
                    //Use boolean as 0 or 1 for precision
                    (int)(dvalue < 100.0 && ((unsigned)((dvalue - (unsigned)dvalue) * 10) > 0)),
                    dvalue, [suffix objectAtIndex:index]];
NSLog(@"Value: %@", svalue);

ARC Localized Version

unsigned long long value = 1700llu;
//value = 32600001llu;
//value = UINT64_MAX;

NSUInteger index = 0;
double dvalue = (double)value;
//Updated to use correct SI Symbol ( http://en.wikipedia.org/wiki/SI_prefix )
NSArray *suffix = @[ @"", @"k", @"M", @"G", @"T", @"P", @"E" ];

while ((value/=1000) && ++index) dvalue /= 1000;

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
//Germany Example
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"de-de"]];
//Set fractional digits to 0 or 1
[formatter setMaximumFractionDigits:(int)(dvalue < 100.0 && ((unsigned)((dvalue - (unsigned)dvalue) * 10) > 0))];

NSString *svalue = [[formatter stringFromNumber:[NSNumber numberWithFloat:dvalue]]
                    stringByAppendingString:[suffix objectAtIndex:index]];

NSLog(@"Value: %@", svalue);
like image 183
Joe Avatar answered Oct 12 '22 12:10

Joe


If you want a string with auto-adjustable width you can use:

+(NSString*)numberWithShortcut:(NSNumber*)number
{
    unsigned long long value = [number longLongValue];

    NSUInteger index = 0;
    double dvalue = (double)value;

    NSArray *suffix = @[ @"", @"K", @"M", @"B", @"T", @"P", @"E" ];

    while ((value /= 1000) && ++index) dvalue /= 1000;

    NSString *svalue = [NSString stringWithFormat:@"%@%@",[NSNumber numberWithDouble:dvalue], [suffix objectAtIndex:index]];

    return svalue;
}
like image 31
user2060883 Avatar answered Oct 12 '22 14:10

user2060883