objective c - Inserting a comma for integers when there are three digits added everytime to the textfield in iOS -
i need add comma separating numbers 3 digits without white spaces , if total digits under three, no comma added.
for example:
2984 => 2,984 297312984 => 297,312,984 298 => 298
how solve this?
tried this:
if([textfield.text length] > 3) { nsmutablestring *stringtext = [nsmutablestring stringwithstring:textfield.text]; [stringtext insertstring:@"," atindex:0]; }
abt after starting, started think if there better solution? because there lot of if-else statements.
welcome suggestion.
this i've come with: set delegate of text field, , implement following method accordingly:
- (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; }
Comments
Post a Comment