How to put comma and decimals in my UITextField dynamically?

柔情痞子 提交于 2019-12-23 02:48:08

问题


I want to add a ',' in '11000' like this '11,000' and decimal ('.') in 465 like 4.65. I wrote this for 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;
}

And what this doing is it actually putting the comma like this 1,1000 for 11000. And i am not able to do anything close for decimal. Please help!!


回答1:


NSNumberFormatter can handle this conversion from string to number and back for you. No need to strip characters yourself with stringByReplacingOccurrencesOfString or use less lenient string to numeric conversion methods like intValue (and at the very least don't use intValue if you want to be able to get a decimal).

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];

NSNumber *amount = [formatter numberFromString:textField.text];

textField.text = [formatter stringFromNumber:amount];

Depending on the input you need to tolerate you might still want to do some other cleanup of the input string if NSNumberFormatter's lenient setting is not enough. You could also use multiple number formatters if you wanted to parse an input string in one format and then output it in another.




回答2:


Use the following code to manually add commas at the right locations. You can get the logic from this code and tweak it to suit your requirement.

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


来源:https://stackoverflow.com/questions/12375363/how-to-put-comma-and-decimals-in-my-uitextfield-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!