텍스트 필드에 숫자가 표시됩니다. "1234"로 번호를 표시하지만 "12345"로 표시되는 다른 큰 번호를 입력하면 "1,234"의 형식으로 표시하고 싶지만 123456을 입력하면 "12,345"로 표시하고 싶습니다. "123,456"으로 표시하십시오. 이 번호를 원하는 형식으로 어떻게 포맷합니까?번호를 어떻게 표시 할 수 있습니까?
-(void)clickDigit:(id)sender
{
NSString * str = (NSString *)[sender currentTitle];
NSLog(@"%@",currentVal);
if([str isEqualToString:@"."]&& !([currentVal rangeOfString:@"."].location == NSNotFound))
{
return;
}
if ([display.text isEqualToString:@"0"])
{
currentVal = str;
[display setText:currentVal];
}
else if([currentVal isEqualToString:@"0"])
{
currentVal=str;
[display setText:currentVal];
}
else
{
if ([display.text length] <= MAXLENGTH)
{
currentVal = [currentVal stringByAppendingString:str];
NSLog(@"%@",currentVal);
[display setText:currentVal];
}
currentVal=display.text;
}
}
이것은 텍스트 필드에 숫자를 표시하는 데 사용하는 코드입니다.
편집 : 나는 다음에 내 코드를 변경하지만 여전히 올바른 형식 숫자하지 않는 :
if ([display.text length] <= MAXLENGTH) {
currentVal = [currentVal stringByAppendingString:str];
NSNumberFormatter * myNumFormatter = [[NSNumberFormatter alloc] init];
[myNumFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *tempNum = [myNumFormatter numberFromString:currentVal];
NSLog(@"My number is %@",tempNum);
[display setText:[tempNum stringValue]];
currentVal=display.text;
}
http://stackoverflow.com/questions/5406366/formatting-a-number-to-show-commas-and-or-dollar-sign-에서 허용되는 답변 http://stackoverflow.com에서 질문을 확인하십시오./a/5407103/928098 당신의 문제를 해결할 것 같습니다. –
OP는 링크 된 답변에서와 같이 표시된 문자열에 달러 기호가 필요하지 않습니다. – sch