1

我想在 '11000' 中添加一个 ',' 像这样的 '11,000' 和十进制 ('.') 在 465 像 4.65。我为 Comma 写了这个:

- (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。而且我无法做任何接近小数的事情。请帮忙!!

4

2 回答 2

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设置不够,您可能仍希望对输入字符串进行一些其他清理。如果您想以一种格式解析输入字符串然后以另一种格式输出,您也可以使用多个数字格式化程序。

于 2012-09-11T18:33:45.547 回答
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;
}
于 2014-12-05T06:47:12.783 回答