2012-08-16 121 views
5

用戶將輸入美元值作爲int,我想將結果轉換爲縮短的格式化字符串。所以如果用戶輸入1700,字符串會說「$ 1.7k」。如果用戶輸入32600000,字符串將會說「$ 32.6m」。將int轉換爲縮短的格式化字符串

更新

這裏是我到目前爲止的代碼。它似乎正在爲數​​字〜10K工作。我只是添加更多的if語句爲更大的數字。但是,有沒有更有效的方法來做到這一點?

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); 
+1

你可以做到這一點一個簡單的if ... else if ... – Selkie 2012-08-16 19:09:16

+2

我相信你在問你的問題之前已經嘗試了一些東西,但是你的代碼沒有工作。你可以請你盡最大努力嗎? – dasblinkenlight 2012-08-16 19:09:53

+1

雖然你會如何得到小數點?即,將1700轉換爲1.7。我認爲這就是我正在努力的。在將其轉換爲字符串之前將其分開? – bmueller 2012-08-16 19:12:53

回答

14
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本地化版本

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); 
+1

哇,這個作品完美。先生,你是個紳士和學者! – bmueller 2012-08-16 22:29:22

+0

我注意到一個問題 - 當指定的數字是'324,333'時,它輸出'324.3k'。這是我想要適應這個空間的太多字符。在這個特定情況下,我怎樣才能擺脫小數點後的所有內容? – bmueller 2012-08-17 18:16:50

+1

'。* f'中的星號('*')決定小數位數。查看我對字符串格式的更新以確保值<100(3個字符)。 – Joe 2012-08-17 18:46:23

7

如果你想自動調節寬度的字符串,你可以使用:

+(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; 
} 
+0

謝謝。尼斯整潔的版本。我喜歡。 – mylogon 2015-05-05 22:56:43