Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I limit the number of decimal points in a UITextField?

I have a UITextField that when clicked brings up a number pad with a decimal point in the bottom left. I am trying to limit the field so that a user can only place 1 decimal mark

e.g.
2.5 OK
2..5 NOT OK

like image 650
user1282180 Avatar asked May 01 '12 20:05

user1282180


People also ask

How do I limit the number of decimal places?

Select the cell with a number (B2) and in the Ribbon, go to Home > Number Format. 2. In the Format Cells window, enter the number of decimal places (for example, 3) and click OK. You can immediately see how the number will look in the Sample box.

How do you reduce the number of decimal places in a cell?

Select the cells you want to format. On the Home tab, select Increase Decimal or Decrease Decimal to show more or fewer digits after the decimal point. Each selection or click adds or removes a decimal place.

How do you limit decimal places to 2?

Rounding a decimal number to two decimal places is the same as rounding it to the hundredths place, which is the second place to the right of the decimal point. For example, 2.83620364 can be round to two decimal places as 2.84, and 0.7035 can be round to two decimal places as 0.70.

How do I restrict decimal places in C++?

The C++ setprecision can also be used to format only the decimal places instead of the whole floating-point or double value. This can be done using the fixed keyword before the setprecision() method.


2 Answers

Implement the shouldChangeCharactersInRange method like this:

// Only allow one decimal point // Example assumes ARC - Implement proper memory management if not using. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {     NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];     NSArray  *arrayOfString = [newString componentsSeparatedByString:@"."];      if ([arrayOfString count] > 2 )          return NO;      return YES; } 

This creates an array of strings split by the decimal point, so if there is more than one decimal point we will have at least 3 elements in the array.

like image 128
lnafziger Avatar answered Oct 03 '22 14:10

lnafziger


Here is an example with a regular expression, the example limits to only one decimal point and 2 decimals. You can tweak it to fit your needs.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {     NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];     NSString *expression = @"^[0-9]*((\\.|,)[0-9]{0,2})?$";     NSError *error = nil;     NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionCaseInsensitive error:&error];     NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString options:0 range:NSMakeRange(0, [newString length])];     return numberOfMatches != 0; } 
like image 33
nizx Avatar answered Oct 03 '22 15:10

nizx