Objective c 如何在我的UITextField中动态放置逗号和小数?

Objective c 如何在我的UITextField中动态放置逗号和小数?,objective-c,ios,uitextfield,nsnumberformatter,Objective C,Ios,Uitextfield,Nsnumberformatter,我想在11000中添加一个“,”,比如这个“11000”,在465中添加一个小数(“.”),比如4.65。 我是为逗号写的: - (BOOL) textField: (UITextField *)textField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string { NSString *unformattedValue; NSNumberFormatter *f

我想在11000中添加一个“,”,比如这个“11000”,在465中添加一个小数(“.”),比如4.65。 我是为逗号写的:

- (BOOL) textField: (UITextField *)textField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {
    NSString *unformattedValue;
    NSNumberFormatter *formatter;
    NSNumber *amount;

    switch ([textField tag]) {
        case 102:
            unformattedValue = [textField.text stringByReplacingOccurrencesOfString:@"," withString:@""];
            unformattedValue = [unformattedValue stringByReplacingOccurrencesOfString:@"." withString:@""];

            formatter = [[NSNumberFormatter alloc] init];
            [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
            [formatter setGroupingSeparator:@","];
            [formatter setDecimalSeparator:@"."];

            amount = [NSNumber numberWithInteger:[unformattedValue intValue]];
            textField.text = [formatter stringFromNumber:amount];
            break;
        default:
            break;
    }
    return YES;
}
这实际上是把逗号放在11000和11000之间。我不能做任何接近小数点的事情。
请帮忙

NSNumberFormatter
可以为您处理从字符串到数字的转换。无需使用
stringByReplacingOccurrencesOfString
或使用不太宽松的字符串到数字转换方法,如
intValue
(如果希望获得小数,至少不要使用
intValue


根据您需要容忍的输入,如果
NSNumberFormatter
lencient
设置不够,您可能还需要对输入字符串执行其他清理操作。如果要以一种格式解析输入字符串,然后以另一种格式输出,还可以使用多个数字格式化程序。

使用以下代码在正确的位置手动添加逗号。您可以从该代码中获取逻辑,并对其进行调整以满足您的需求

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

NSLog(@"Text:%@textField length:%dRange.length:%lu , Range.location:%lu :: replacementString:%@",textField.text,textField.text.length,(unsigned long)range.length,(unsigned long)range.location,string);
NSMutableString *tempString=textField.text.mutableCopy;
int digitsCount;

if ([string isEqualToString:@""])    //digit removed
{
    NSLog(@"digit removed, string length after trimming:%d",[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].length);
    digitsCount=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].length-1; //digit removed
}else   ///digit added
{
digitsCount=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].length+1 ;
}
NSLog(@"Number of digits:%d",digitsCount);
switch (digitsCount)
{
    //case 1:textField.text=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""];
        //break;
    case 3:textField.text=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""];
        break;

    case 4:
        //remove previous comma...
        tempString=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].mutableCopy;
        [tempString insertString:@"," atIndex:1];
        textField.text=tempString;
            break;

    case 5:
         //remove previous comma...
        tempString=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].mutableCopy;
        [tempString insertString:@"," atIndex:2];
        textField.text=tempString;
        break;

    case 6:
        //remove previous comma...
        tempString=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].mutableCopy;
        [tempString insertString:@"," atIndex:1];
        [tempString insertString:@"," atIndex:4];
        textField.text=tempString;

        break;

    default:
        break;
}

return YES;
}

调用
[unformattedValue intValue]
将始终为您提供一个整数值,您将从输入中删除小数点后的任何内容,因此您永远不会在输出中看到它。
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

NSLog(@"Text:%@textField length:%dRange.length:%lu , Range.location:%lu :: replacementString:%@",textField.text,textField.text.length,(unsigned long)range.length,(unsigned long)range.location,string);
NSMutableString *tempString=textField.text.mutableCopy;
int digitsCount;

if ([string isEqualToString:@""])    //digit removed
{
    NSLog(@"digit removed, string length after trimming:%d",[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].length);
    digitsCount=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].length-1; //digit removed
}else   ///digit added
{
digitsCount=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].length+1 ;
}
NSLog(@"Number of digits:%d",digitsCount);
switch (digitsCount)
{
    //case 1:textField.text=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""];
        //break;
    case 3:textField.text=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""];
        break;

    case 4:
        //remove previous comma...
        tempString=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].mutableCopy;
        [tempString insertString:@"," atIndex:1];
        textField.text=tempString;
            break;

    case 5:
         //remove previous comma...
        tempString=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].mutableCopy;
        [tempString insertString:@"," atIndex:2];
        textField.text=tempString;
        break;

    case 6:
        //remove previous comma...
        tempString=[tempString stringByReplacingOccurrencesOfString:@"," withString:@""].mutableCopy;
        [tempString insertString:@"," atIndex:1];
        [tempString insertString:@"," atIndex:4];
        textField.text=tempString;

        break;

    default:
        break;
}

return YES;
}