0

我将如何在文本字段中设置货币以将其显示为本地化货币,以 0 开头。如果有人输入 16.25 便士,它将分别格式化为 0.1625£。我正在使用委托并格式化所有文本字段,因此只能传入数字,该字段也应该本地化。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range     replacementString:(NSString *)string  {    // First, create the text that will end up in the     input field if you'll return YES:
    NSString *resultString = [textField.text stringByReplacingCharactersInRange:range    withString:string];
    // Now, validate the text and return NO if you don't like what it'll contain.
// You accomplish this by trying to convert it to a number and see if that worked.
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
   [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber* resultingNumber = [numberFormatter numberFromString:resultString];
    //[numberFormatter release];
    return resultingNumber != nil;   

我不希望这改变,因为它格式化了我所有的字段。只希望 textField1 具有相关格式,我将如何执行此操作,我认为它在于 viewdidload 方法并将文本属性设置为本地化为浮点,但我似乎无法弄清楚如何去做。

4

2 回答 2

0

您可以在上面的委托方法中指定要格式化的 textField。

if (textField == textField1) {

   // Do Something....

} else {

  // Do whatever you want with the other text fields

}

对于浮点格式,使用这样的东西 -

[myTextField setText:[NSString stringWithFormat:@"%.2f", myFloat]];
于 2013-05-08T23:18:02.387 回答
0
  • (void)textFieldDidEndEditing:(UITextField *)textField

{如果(文本字段1){

    NSString *txt = self.textfield1.text;

    double num1 = [txt doubleValue];


    double tCost = num1 /100;





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

    [numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];


    NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:tCost]];


    self.textfield1.text = [NSString stringWithFormat:@"%@",numberAsString];
}

}

于 2013-05-09T12:52:59.057 回答