3

我需要将逗号分隔数字添加为三位数字,没有空格,如果总数字小于三,则不添加逗号。

例如:

2984 => 2,984
297312984 => 297,312,984
298 => 298

我该如何解决这个问题?

试过这个:

if([textfield.text length] > 3)
{
 NSMutableString *stringtext = [NSMutableString stringWithString:textfield.text];
 [stringtext insertString:@"," atIndex:0];
}

abt开始后,开始思考是否有更好的解决方案?这是因为会有很多 if-else 语句。

欢迎提出任何建议。

4

4 回答 4

3

这就是我想出的:只需设置文本字段的委托,并相应地实现以下方法:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text.length % 4 == 3) {
        if (string.length != 0) {
            textField.text = [NSString stringWithFormat:@"%@,%@", textField.text, string];
            return NO;
        }
    }

    return YES;
}
于 2013-04-05T06:48:09.510 回答
1

使用 NSNumberFormatter,

NSNumberFormatter *formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; // this line is important!

NSString *formatted = [formatter stringFromNumber:[NSNumber numberWithInteger:2000000]];

[formatter release];
于 2013-04-05T06:38:29.527 回答
1

使用NSNumberFormatter

NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *commaString = [numberFormatter stringFromNumber:[NSNumber numberWithInteger:yourIntegerValue]];
NSLog(@"---> %@",commaString);

或通过字符串操作 <This is not the best way>

NSMutableString *mutString=[NSMutableString stringWithString:@"123456789"];

NSMutableArray *array=[NSMutableArray new];
for (NSInteger i=1; i<=mutString.length/3; i++) {
    NSInteger index=[mutString length]-i*3;
    [array insertObject:[mutString substringWithRange:NSMakeRange(index, 3)] atIndex:0];
}
if (mutString.length!=mutString.length/3*3) {
    [array insertObject:[mutString substringWithRange:NSMakeRange(0, mutString.length%3)] atIndex:0];
}
NSString *commaString=[array componentsJoinedByString:@","];
NSLog(@"--> %@",commaString);
于 2013-04-05T06:42:04.437 回答
0

请尝试以下功能

    //------------------------------------------------------------------------
    // Method : ChangeNumberIntoCommaSeparatedStringDec
    // Method to convert number into comma separated string
    //------------------------------------------------------------------------
    -(NSString *)ChangeNumberIntoCommaSeparatedStringDec:(double)num
    {

            NSNumber *number = [NSNumber numberWithDouble:num];
            NSNumberFormatter *frmtr = [[NSNumberFormatter alloc] init];
           //here locale as applied to do not change the formatter values after changing the device locale
            [frmtr setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"]];
            [frmtr setGroupingSize:3];
            [frmtr setGroupingSeparator:@","];
            [frmtr setUsesGroupingSeparator:YES];
            [frmtr setMaximumFractionDigits:2];
            [frmtr setMinimumFractionDigits:2];
            NSString *str=[frmtr stringFromNumber:number];
            [frmtr release];
            return(str);        
    }
于 2013-04-05T06:57:08.750 回答