UITextField to hold upto 10 numbers before decimal place and upto three numbers after decimal

swift float 2 decimal places
uitextfield allow only one decimal point swift
swift string(format 2 decimal places)
allow only numbers in uitextfield swift 4
swift string to double-precision
swift number formatter 2 decimal places
allow only alphabets in uitextfield swift
swift numberformatter

I am looking for a code that allows me to hold upto 10 numbers before decimal place and upto three numbers after decimal.

Valid examples are: 1234567899.123 - maximum 10 numbers before decimal and 3 after decimal position.

123.123 - less than 10 numbers before decimal okay.

123456.1 - less than 3 numbers after decimal okay.

123 - Okay

I am using this function below:

-(BOOL)textField: (UITextField*)tectField shouldChangeCharactersInRange: (NSRange)range replacementString:(NSString*) string {

    NSString *newStrn = [textField.text stringReplacingCharactersInRange: range withString: string];

    NSArray *arrayofStrn = [newStrn componentsSeparatedByString:@"."]; //to keep only one decimal
    if([arrayofStrn count]> 2) {

        return NO;
    else {

        return YES;

Do not know how to proceed further to keep max limit of 10 numbers (less than 10 okay) before decimal and maximum limit of 3 numbers after decimal (less than 3 numbers or no numbers after decimal okay).

Thanks very much guys!

You could achieve this with a simple regex like this

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
  NSString *editedString = [textField.text stringReplacingCharactersInRange:range withString:string];

  NSString    *regex     = @"\\d{0,10}(\\.\\d{0,3})?"
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

  return [predicate evaluateWithObject:editedString];

I've tested the above with the following inputs

@"1234567891",      //=> YES
@"12345678912",     //=> NO
@"1234567891.122",  //=> YES
@"1234567891.1234", //=> NO
@".123",            //=> YES

How to specify floating-point precision in a string, For example, if a number is 45.6789, you might only want to show two digits after the decimal place. Here's an example using basic string  Select the cells you want to limit the number of decimal places. 2. Right click the selected cells, and select the Format Cells from the right-clicking menu. 3. In the coming Format Cells dialog box, go to the Number tab, click to highlight the Number in the Category box, and then type a number in the Decimal Places box.

Inside your else statement put the following:

if([arrayofStrn count] < 2) {
    if ([[arrayofStrn objectAtIndex:0] length] > 10) {
    } else {
} else {
    if ([[arrayofStrn objectAtIndex:0] length] > 10 || [[arrayofStrn objectAtIndex:1] length] > 3) {
    } else {


Just as a note, you can retrieve the length of NSString like so:

theString.length or [theString length]

How to verify and limit decimal number inputs in iOS with Swift, Apple only provides UITextField for inputs which always returns a only allows valid decimal numbers and limits the amount of decimal any way (typing, deleting, pasting) and before the UI reflects the changes. If it is two then the second part is the one after // the separator, aka the digits we care about. I need some regex that will match only numbers that are decimal to two places. For example: 123 = No match 12.123 = No match 12.34 = Match

Here's Xcode 10 and Swift 4.2

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    if textField == txt_Amount{
        let amountString: NSString = textField.text! as NSString
        let newString: NSString = amountString.replacingCharacters(in: range, with: string) as NSString
        let regex = "\\d{0,6}(\\.\\d{0,2})?"

        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with:newString)

    return true

How to program an iOS text field that takes only numeric input or , That means it will accept the following characters: The digits 0 through 9; The – (​negative) symbol; The decimal separator, which is either . or ,  Yep. To round after k decimal places, use a scale factor of 10^k. This should be really easy to see if you write out some decimal values by hand and play around with multiples of 10. Suppose you're working with the value 1.23456789, and want to round it to 3 decimal places. The operation available to you is round to integer. So, how do you move

How to set decimal places in java, Nov 10, 2017 · Java DecimalFormat Example Jerry Zhao November 10, 2017 0 We often Decimal Number with maximum up to two decimal places validation will be To set the number of digits after the decimal, use the . javacodex. with three decimal places; Formatting number with two decimal places; How to tell if a​  However many decimal places we have, that's the number of zeroes for the number matching the "place" we're at. For instance, the number 83.295 has three digits after the decimal place (the digits 2, 9, and 5), so the number has three decimal places. The number with "1" followed by three zeroes is 1,000: one thousand. That means that "83.295" is

Configuring UITextField using UITextContentType and , In iOS 10, Apple introduced the concept of the "text content type" -- or to which you've probably encountered at some point already. Two dashes becomes en-​dash, and three dashes becomes em-dash. "numbers and punctuation" keyboard for number, phone and decimal pad inputs. Stay up to date. When you run the program, the output will be: In the above program, we've used format () method to print the given floating point number num to 4 decimal places. The 4 decimal places is given by the format .4f. This means, print only upto 4 places after the dot (decimal places), and f means to print the floating point number.

Digit Only Directive in Angular, Natural numbers are frequently used as Account Numbers, This post will describe an Angular directive digitOnly that we are using to Before jumping into the code, let's discuss an alternative solution, -moz-appearance: textfield; the number input box accepts values containing decimal points and  num = 10.234560 In this output the number of digits after decimal are 6, which is default format of float value printing. Now, we want to print 2 digits only after decimal. Use "%.nf" format specifier. By using this format specifier we can print specific number of digits after the decimal, here "n" is the number of digits after decimal point.

  • Do you want the numbers to round ? or just not allow entry ?
  • Not allowing entry is fine, no need to round it.
  • Is the actual code in use? I ask because the method name is componentsSeparatedByString
  • @David thats just for future use to check for only one decimal place. in if([arrayofStrn count] > 2)
  • My point being that the code you've posted won't compile because of a spelling error. Your best results from SO are going to be achieved by putting out a best faith effort at solving the problem and then posting a specific question.
  • genius man!! Thats the closest logic for my question. This code works perfect, but allows me to put 14 numbers straight if I do not use decimal. I need only 10 numbers in all if I do not use decimal. Cool man, I will learn more about regex.
  • Ooops was missing another backslash before the period - updated and should work now
  • Nopes! the another backslash does not allow me type anything.
  • Sorry I actually tried it this time - my bad. I've made everything from the period and on optional
  • very close man!! Now it allows 10 characters before decimal but does not allow me to to type anything after decimal.
  • I get NSRangeException error when hitting a backspace! :( index 1 beyond bounds [0..0]
  • if([arrayofStrn count]> 2) should be good for checking decimal? if this is not true then we perform your code:if ([[arrayofStrn objectAtIndex:0] length] > 10 || [[arrayofStrn objectAtIndex:1] length] > 3) { //Bad } else { //Good }
  • Need two checks. First to see if array has more than two objects, then to see if array has just one or two objects. If just one, test against 10. If 2, test against 10 and 3