开发者

Obj-C, iOS, how do I format a UITextField for currency international?

开发者 https://www.devze.com 2023-03-02 16:26 出处:网络
I need to format a numeric value in a UITextField which will format as number is entered. I need the value to format the price internationally, dependant on the phones currency setting.

I need to format a numeric value in a UITextField which will format as number is entered. I need the value to format the price internationally, dependant on the phones currency setting.

Help!

Edit, this is what I have at the moment, which works in the US and UK, but causes me problems with other currencies like the Euro.

+(NSString*)addFormatPrice:(double)dblPrice {   
  NSNumber *temp = [NSNumber numberWithDouble:dblPrice];
  NSDecimalNumber *someAmount = [NSDecimalNumber decimalNumberWithDecimal:
    [temp decimalValue]];   
  NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] 
    autorelease];
  [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];    
  return [currencyFormatter stringFromNumber:someAmount];
}

+(double)removeFormatPrice:(NSString *)strPrice {   
  NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] 
    autorelease];
  [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
  NSNumber* number = [currencyFormatter numberFromString:strPrice];
  return [number doubleValue];
}

.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:
    (NSRange)range replacementString:(NSString *)string {
  double currentValue = [General removeFormatPrice:textField.text];
  double cents = round(currentValue * 100.0f);

  if (([string isEqualToString:@"."]) && ((int)currentValue == 0)) {
    cents = floor(cents * 100);
  } else 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);
  }

  NSString *str = [NSString stringWithFormat:@"%f", cents];
  if ([str length] > 15) {
    NSString *newStr = [str substringFromIndex:1];
    cents = [newStr doubleValue];
  }

  textField.text = [General addFormatPrice:[[NSString 
    stringWithF开发者_Go百科ormat:@"%.2f", cents / 100.0f] doubleValue]];

  return NO;

}


NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];

NSString *currencyString = [formatter stringFromNumber:yourNumberHere];

You'll kill yourself trying to get it correct as you enter it for any currency. You'll have to use the UITextField delegate method

  • (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

to intercept changes, then try to convert the new string back into a number using the formatter. But that might not work if they do something that confuses the formatter's parsing, like delete the currency symbol or skip a comma.

So then you'll have to try to parse the number by hand, for all currencies.

It's far easier if you just leave off the currency formatting while they edit, then format the result when they're done.


Use NSNumberFormatter. Using the NSNumberFormatterCurrencyStyle will set the formatter to the current locale. You can also change the locale to something.

0

精彩评论

暂无评论...
验证码 换一张
取 消