How do you dynamically format a number to have commas in a UITextField entry?

前端 未结 9 2069
傲寒
傲寒 2020-12-08 17:42

I want to have commas dynamically added to my numeric UITextField entry while the user is typing.

For example: 123,456 and 12,345,678

9条回答
  •  臣服心动
    2020-12-08 18:11

    Format the number with grouping attributes as shown here.

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setGroupingSeparator:@","];
    [numberFormatter setGroupingSize:3];
    [numberFormatter setDecimalSeparator:@"."];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setMaximumFractionDigits:3];
    [numberFormatter setMinimumFractionDigits:3];
    

    Output for the above code is

    1,234,567.850
    

提交回复
热议问题