2010-08-05 100 views
1

我已經得到了這個基本的腳本,我需要轉換爲目標c,它將大量單位的資金轉換爲縮短版本(即:1.2m等)已經完成了大部分轉換,但我遇到的最大問題是最後。將基本腳本轉換爲Objective C(貨幣格式)

原來的基本代碼是:

; Basic Code 

Function ShortCash$(BigNumber) 
out$="" 


    ; First, grab the length of the number 
    L=Len(BigNumber) 

    Letter$="" 

    ;Next, Do a sweep of the values, and cut them down. 
     If l<13 
      out$=(BigNumber/1000000000) 

      ; For each figure, out remainder should be divided so that it leaves a 2 digit decimal number.. 
      remainder=(BigNumber Mod 1000000000)/10000000 

      ; And we also want a letter to symbolise our large amounts.. 
      Letter$="b" ; BILLION!!!! 
     EndIf 
     If l<10 Then out$=(BigNumber/1000000):remainder=(BigNumber Mod 1000000)/10000:Letter$="m" 
     If l<7 Then out$=(BigNumber/1000):remainder=(BigNumber Mod 1000)/10:Letter$="k" 
     If l<4 Then out$=BigNumber:remainder=0:Letter$="" 

    ;Next, if remainder=0 then we're happy.. ie, £1m is fine, we need no decimal. 
    ;But, if the remainder is >0 we'll want a nice rounded 2 decimal number, instead. 
    If remainder>0 
     out$=out$+"."+Right$("00"+remainder,2) ; Last two numbers.. 

     ; Additionally, if the rightmost figure is a 0, remove it. 
     ; (ie, if the value is 1.50, we don't need the 0) 
     If Right$(out$,1)="0" Then out$=Left$(out$,Len(out$)-1) 

    EndIf 


    ; And throw on our letter, at the end. 
    out$=out$+letter$ 

Return out$ 
End Function 

//下面是靠郵政的作者在週四8月5日編輯。

我相信我已經把它整理好了,現在我有以下幾千個工作,我不確定它是否能在所有情況下都能正常工作,並且歡迎任何幫助/指導。我意識到內存問題,我稍後會解決它,它是我首先解決的字符串操作部分。

// This goes inside the (IBAction) update method; 

    NSNumber *bigNumber = nil; 

    if ([inputField.text length] >0) 
    { 
     bigNumber = [NSNumber numberWithInt:[inputField.text intValue]]; 
    } 

    int bigNumberAsInt   = [bigNumber intValue]; 
    NSString *bigNumberAsString = [bigNumber stringValue]; 
    int bigNumberStrLen   = [bigNumberAsString length]; 

    NSLog(@"bigNumber = %@", bigNumber); 
    //NSLog(@"bigNumberAsString = %@", bigNumberAsString); 
    NSLog(@"bigNumberStrLen = %d", bigNumberStrLen); 
    NSLog(@"========="); 

    // ========= 

    NSNumberFormatter *nformat = [[[NSNumberFormatter alloc] init] autorelease]; 

    [nformat setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
    [nformat setCurrencySymbol:@"$"]; 
    [nformat setNumberStyle:NSNumberFormatterCurrencyStyle]; 
    [nformat setMaximumFractionDigits:0]; 

    NSLog(@"Cash = %@", [nformat stringFromNumber:bigNumber]); 

    // ========= 


    NSString *output = [[NSString alloc] init]; 
    NSString *letter; 


    // ========== 

    // Anything less than 1m represent with a k 
    if (bigNumberStrLen < 7) 
    { 
     letter = @"k"; 
     int sum = (bigNumberAsInt/1000); 
     int int_remainder = ((bigNumberAsInt % 1000)/10); 

     NSLog(@"Remainder = %d", int_remainder); 

     NSString *sumAsString = [NSString stringWithFormat:@"%d", sum]; 
     NSString *remainderAsString = [NSString stringWithFormat:@"%d", int_remainder]; 

     NSLog(@"Sum as String = %@", sumAsString); 
     NSLog(@"Remainder as String = %@", remainderAsString); 

     if (int_remainder >0) 
     { 
      NSLog(@"Remainder > 0"); 

      output = [output stringByAppendingString:sumAsString]; 
      output = [output stringByAppendingString:@"."]; 
      output = [output stringByAppendingString:remainderAsString]; 

      NSLog(@"Output = %@", output); 

      NSUInteger outputStrLen = [output length]; 

      NSLog(@"Output strlen = %d", outputStrLen); 

      if ([output hasSuffix:@"0"]) 
      { 
       NSLog(@"Has suffix of 0"); 

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

     } 

     output = [output stringByAppendingString:letter]; 

     NSLog(@"Final output = %@", output); 


    } 

這將顯示10.2k(如果它以0後綴結尾)或它將顯示10.2x其中X是最後一個數字。

有人可以仔細檢查一下,或者有更簡單的方法可以做到這一點。無論如何,感謝您的幫助。

回答

0

....好吧,感謝作者Cocoa Tidbits blog,我相信我有一個更優雅,更快速,不需要太多編碼的解決方案;它仍然需要測試,而且它可能還需要多一點編輯,但它似乎比我的原版好得多。

我修改了一下腳本,使其不顯示任何相關的尾部零;

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

@Fabien我這個去解決方案,但感謝您的幫助無論如何 – zardon 2011-05-30 20:22:04

1

只是爲了改進解決方案,一個好主意,也許是子類NSNumberFormatter類和重寫 - (的NSString *)stringForObjectValue:(ID)anObject方法。

使用zardon中的代碼,我添加了一個語句< 1000,它不會格式化數字。

這裏是方法的代碼:

/* 
    Override the stringForObjectValue method from NSNumberFormatter 

    100 -> 100 
    1000 -> 1k 
    1 000 000 -> 1m 
    1 000 000 000 -> 1b 
    1 000 000 000 -> 1t 
*/ 
- (NSString *)stringForObjectValue:(id)anObject { 

    // If we don't get a NSNumber, we can't create the string 
    if (![anObject isKindOfClass:[NSNumber class]]) { 
     return nil; 
    } 

    NSNumberFormatter *nformat = [[NSNumberFormatter alloc] init]; 

    // Decimal value from the NSObject 
    double doubleValue = [anObject doubleValue]; 

    NSString *stringValue = nil; 

    // Abbrevations used 
    NSArray *abbrevations = [NSArray arrayWithObjects:@"k", @"m", @"b", @"t", nil] ; 

    // If the value is less than 1000, we display directly the value 
    if(doubleValue < 1000.0) { 
     stringValue = [NSString stringWithFormat: @"%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]] ]; 
    } 
    else { // Otherwise we format it as expected 
     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]; 

       break; 
      } 
     } 
    } 

    [nformat release]; 

    return stringValue; 
} 

在該界面中,我們只需添加的繼承聲明:

@interface MoneyNumberFormatter : NSNumberFormatter 

希望這有助於..

+0

儘管我使用的是我之前發佈的解決方案,但我認爲您的子類化想法是一個非常好的擴展/想法。 – zardon 2011-05-30 20:23:03