如何动态地在我的UITextField中放入逗号和小数?

时间:2012-09-11 18:02:53

标签: objective-c ios uitextfield nsnumberformatter

我想添加一个','在 11000'喜欢这个 11,000' 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;
}

这样做实际上就是将这个逗号像1,1000一样放在11000.而且我无法做任何接近十进制的事情。 请帮忙!!

2 个答案:

答案 0 :(得分:2)

NSNumberFormatter可以处理从字符串到数字的转换,然后返回给您。无需使用stringByReplacingOccurrencesOfString自行删除字符,也不需要使用较少的宽松字符串来处理intValue之类的数字转换方法(如果您希望能够获得intValue,则至少不要使用NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; NSNumber *amount = [formatter numberFromString:textField.text]; textField.text = [formatter stringFromNumber:amount]; 小数)。

NSNumberFormatter

根据您需要容忍的输入,如果lenient的{​​{1}}设置不够,您可能仍希望对输入字符串进行其他一些清理。如果要以一种格式解析输入字符串,然后将输出字符串输出到另一种格式,也可以使用多个数字格式化程序。

答案 1 :(得分:0)

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

-(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;
}