2010-09-26 48 views
1

我找到了下面的代碼,我試圖在UITableView的shouldChangeCharactersInRange事件中工作。沒有貨幣符號,它工作正常。iPhone:如何在shouldChangeCharactersInRange中添加貨幣符號,小數點和小數位

當我嘗試添加貨幣符號時,它只是允許我輸入一個數字。它應該做的是這樣的,比如說我輸入7536,它應該出現在以下步驟中:£0.07,£0.75,£7.53,£75.36,而不是0.07,£0.05,£0.03,£0.06

繼承人的代碼。

- (BOOL)textField:(UITextField *)textField 
     shouldChangeCharactersInRange:(NSRange)range 
     replacementString:(NSString *)string 
{ BOOL res = TRUE; 

    double currentValue = [textField.text doubleValue]; 
    double cents = round(currentValue * 100.0f); 

    if ([string length]) { 
     for (size_t i = 0; i < [string length]; i++) { 
      unichar c = [string characterAtIndex:i]; 
      if (isnumber(c)) { 
       cents *= 10; 
       cents += c - '0'; 
      }    
     } 
    } else { 
     // back Space 
     cents = floor(cents/10); 
    } 

    // This line next works without the currency symbol 
    //textField.text = [NSString stringWithFormat:@"%.2f", cents/100.0f]; 

    NSMutableString *aString = [NSMutableString stringWithFormat:@"%@", strCurrencySymbol];  
    [aString appendFormat:@"%.2f", cents/100.0f]; 
    textField.text = aString; 

    res = NO; 
return res; 

}

回答

0
NSCharacterSet *currencySet = [NSCharacterSet characterSetWithCharactersInString:@"$€£¥"]; 
    NSMutableString *strSource = [NSMutableString stringWithString:textField.text]; 
    if (![strSource isEqualToString:@""]) { 
     [strSource replaceCharactersInRange: [strSource rangeOfCharacterFromSet:currencySet] withString:@""]; 
    } 

    double currentValue = [strSource doubleValue]; 
    double cents = round(currentValue * 100.0f); 

    if ([string length]) { 
     for (size_t i = 0; i < [string length]; i++) { 
      unichar c = [string characterAtIndex:i]; 
      if (isnumber(c)) { 
       cents *= 10; 
       cents += c - '0'; 
      }    
     } 
    } else { 
     // back Space 
     cents = floor(cents/10); 
    } 

    NSMutableString *aString = [NSMutableString stringWithFormat:@"%@", strCurrencySymbol]; // does not need to be released. Needs to be retained if you need to keep use it after the current function. 
    switch (intDecimalPlaces) { 
     case 0: 
      [aString appendFormat:@"%.0f", cents/100.0f]; 
      break; 
     case 1: 
      [aString appendFormat:@"%.1f", cents/100.0f]; 
      break; 
     case 2: 
      [aString appendFormat:@"%.2f", cents/100.0f]; 
      break; 
     case 3: 
      [aString appendFormat:@"%.3f", cents/100.0f];    
      break; 
     default: 
      break; 
    } 

    textField.text = aString; 

我忘了在開始移除貨幣符號。 然後在最後添加它沒有問題。

0

[@"£1.23" doubleValue]爲0。你如果有一個,像跳過一個主導貨幣符號:

NSString * currentText = textField.text; 
NSRange range = [currentText rangeOfString:strCurrencySymbol]; 
if (range.location == 0) 
{ 
    currentText = [currentText substringFromIndex:range.length]; 
} 
double currentValue = [currentText doubleValue]; 

一般情況下,我建議不要做的東西像這樣:

  • 它可能不適用於所有輸入法(和IIRC shouldChangeCharactersInRange:不會被調用utocompletions)。
  • 除非您在收銀機上工作(他們有一個「00」鍵,否則輸入「$ 100.00」是一種痛苦),這看起來並不方便。我希望能夠輸入「1」,意思是$ 1.00
  • 它不是本地化的。
+0

我不知道我明白你可以給我完整的shouldChangeCharactersInRange函數。我不打算啓用自動完成。另外,我懷疑$ 100.00會經常進入。我將通過不同的貨幣符號和小數位。 – Jules 2010-09-26 19:32:12

+0

在調用'-doubleValue'獲得'currentValue'之前,您需要去掉貨幣符號。我的代碼這樣做。其餘的代碼可以遵循。 – 2010-09-26 22:07:34

+0

開始時的值不會有貨幣符號,整個問題是我想添加它並在每次按鍵後顯示它。對不起,如果不明確。 – Jules 2010-09-27 10:52:26

相關問題