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 메서드에 있고 텍스트 속성을 부동 소수점으로 지역화하도록 설정한다고 생각합니다. 그렇지만 어떻게 할 수 있을지는 생각지 않습니다.

+0

당신이 여기에서 묻고있는 것을 말하기 어렵습니다. – HalR

답변

0
  • (무효) 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]; 
} 

}

0

위의 위임 방법에서 서식을 지정할 textField를 지정할 수 있습니다. 부동 소수점 형식에 대한

if (textField == textField1) { 

    // Do Something.... 

} else { 

    // Do whatever you want with the other text fields 

} 

,이 같은 것을 사용 -

[myTextField setText:[NSString stringWithFormat:@"%.2f", myFloat]]; 
+0

[textfield1 setText : [NSString stringWithFormat : @ "%. 2f", myFloat]]; if else 문을 사용할 수는 있지만 위 텍스트 행을 텍스트 필드 1에 통합하는 방법은 다른 텍스트가 반환되면 대리자 메서드가 nil을 반환한다는 것을 이해했기 때문에 텍스트 필드 1에 대해 어떻게 통합 할 것입니까? 그러나 나는 그것이 nill이 아니고 형식화 된 문자열을 반환해야합니다. –

+0

나는 이렇게했다. 오른쪽 길을 시도했다. \t \t [resultString setText : [NSString stringWithFormat : @ "%. 2f", (float) resultsNumber]]; 나는 float와 포인터를 캐스팅 할 수 없었 recieved. –

+0

ive 내 자신의 방법으로 그것을 할 :) :) - (void) textFieldDidEndEditing : (UITextField *) textField –