9 Replies
      Latest reply: Oct 20, 2016 2:13 PM by patriot RSS
      patriot Level 1 Level 1 (0 points)

        I have two text field that require numeric user input. 

         

        At the moment my code accepts only a single decimal point and two digits after the decimal point. No problems with that.  The issue I would like to address is this:  If the user only enters '125' or '125.' and taps out of the text field, I would like for the text field to show '125.00' in both cases.  So this will involve appending either '.00' or '00' to match the users data input.

         

        If anyone can shed some light on this I would be most grateful.

         

        Thanks!

        • Re: Is there a way to append data to user input data?
          Claude31 Level 4 Level 4 (825 points)

          get the label in s

            

          convert to Double

           

          format :

           

          if let x = Double(s)  {   // check it's a numeric value
                          let formatter = NSNumberFormatter()
                          formatter.minimumFractionDigits = 2
                          formatter.maximumFractionDigits = 2
                         let myString = formatter.stringFromNumber(x)
          }
          

           

          load the textField with myString

          • Re: Is there a way to append data to user input data?
            ahltorp Level 3 Level 3 (410 points)

            Either you use a NumberFormatter directly on the text field, or you parse the string and write back the formatted result with NumberFormatter.


            The relevant fields to set on the NumberFormatter are:

            formatter.numberStyle = .decimal
            formatter.minimumFractionDigits = 2
            formatter.maximumFractionDigits = 2
            formatter.usesGroupingSeparator = false
            

             

            For example, if you do it manually:

             

            let formatter = NumberFormatter()
            formatter.numberStyle = .decimal
            formatter.minimumFractionDigits = 2
            formatter.maximumFractionDigits = 2
            formatter.usesGroupingSeparator = false
            if let text = textField.text, let n = formatter.number(from: text) {
                textField.text = formatter.string(from: n)
            }
            
              • Re: Is there a way to append data to user input data?
                patriot Level 1 Level 1 (0 points)

                Thanks for your post and the code.

                 

                I will take a good look at your code suggestion.  I hope I will be able to make this work with the code I am currently using, which is (in part):

                 

                func textField(textFieldToChange: UITextField,
                                    shouldChangeCharactersInRange range: NSRange,
                                        replacementString string: String) -> Bool {
                   
                        if textFieldToChange == foodnDrinkTextField {
                           
                            let textFieldString = foodnDrinkTextField.text! as NSString
                            let newString = textFieldString.stringByReplacingCharactersInRange(range, withString:string)
                            let floatRegEx = "^([0-9]+)?(\\.([0-9]{1,2})?)?$" / 
                            let floatExPredicate = NSPredicate(format:"SELF MATCHES %@", floatRegEx)
                            return floatExPredicate.evaluateWithObject(newString)
                           
                        } else if textFieldToChange == taxTextField {
                          .
                          .
                          .
                        }
                        return true
                }
                
                

                 

                I will know more later tonight on this.

                 

                Thanks again for your help.

                  • Re: Is there a way to append data to user input data?
                    eskimo Apple Staff Apple Staff (6,310 points)

                    let floatRegEx = "^([0-9]+)?(\\.([0-9]{1,2})?)?$"

                    You do realise that a lot of European countries use comma (,) for separating the fractional part of decimals, right?  So, how you write 1234.56 Euros depends on the user’s locale:

                    • Ireland — €1,234.56

                    • France — 1 234,56 €

                    • Germany — 1.234,56 €

                    ahltorp suggested approach (NumberFormatter) will take care of this for you.

                    Share and Enjoy

                    Quinn “The Eskimo!”
                    Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                    let myEmail = "eskimo" + "1" + "@apple.com"

                  • Re: Is there a way to append data to user input data?
                    patriot Level 1 Level 1 (0 points)

                    I forgot to add in my last post that the use of NSNumberFormatter() also solved my orginal problem which was the addition of '.00' or '00' to the user input.  That solved a big problem for me.

                     

                    The use of NSNumberFormatter() is invaluable IMO.

                     

                    I sincerely appreciate the help that everyone provided me.