Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

objective c - 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); 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...