NSNumberFormatter() code not printing the % symbol?

The heading to this thread is somewhat misleading because in truth I am not certain that NSNumberFormatter() code is actually the culprit in this case. I’ll explain:

My app requires that the user enter four sets of textfield data: food, tax, tip and split values. Because I am using NSNumberFormatter() code, the tip value is followed by the % symbol after the user enters her data, i.e., 17, so the textfield ends up being ‘17%’ after she taps out of the tip textfield.

When the app is first launched and the user enters her data the % symbol appears as it should. However, after she enters all of her data and wishes to clear all of the data fields she can tap the Reset button and the data fields will clear. However, when she re-enters new data the % symbol never appears again. The problem is that the SAME EXACT code is executed in both cases - before or after she presses the Reset button.

Here is the pertinent code:

func textFieldDidEndEditing(textField: UITextField) {  
     formatter.numberStyle = .CurrencyStyle  
     formatter.usesGroupingSeparator = false  
     formatter.locale = NSLocale.currentLocale()

if textField == foodnDrinkTextField {
. . .
} else if textField == taxTextField {
. . .
} else if textField == tipTextField {

        tipTextField.resignFirstResponder()  
        tipCalc.tipPercentValue = Double((tipTextField.text! as NSString).doubleValue)   
        formatter.numberStyle = .PercentStyle  
        formatter.maximumIntegerDigits = 3  
        tipTextField.textColor = UIColor.blueColor() 
        tipTextField.font = UIFont.systemFontOfSize(26, weight: UIFontWeightThin)  
        tipInput = (tipCalc.tipPercentValue / 100)  
        tipTextField.text = formatter.stringFromNumber(tipInput) 
 
    } else if textField == splitTextField { 

The Reset() code is as follows:

@IBAction func reset(sender: AnyObject) {  

    if tipCalc.tipPrefValue > 100 {  

        defaults.removeObjectForKey("foodnDrinkTextField")  
        defaults.removeObjectForKey("taxTextField")  
        defaults.removeObjectForKey("splitTextField")  
 } else {  
        defaults.removeObjectForKey("foodnDrinkTextField")  
        defaults.removeObjectForKey("taxTextField")  
        defaults.removeObjectForKey("tipTextField")/  
        defaults.removeObjectForKey("splitTextField")  
        defaults.synchronize()  /  
    }  

 resetFlags() 
    
    navigationController?.navigationBarHidden = true  
    foodnDrinkTextField.text = ""   
    hideAllLabelsnTextFields()     
} 

For the sake of clarity, the app allows the users to pre-set a tip preference so she will not have to enter it every time she uses the app. In this case, when entering new data the tip textfield is auto-filled with her tip preference and skips over to the split data which is the last textfield requiring data input. So looking at the second code group, the line with the code - if tipCalc.tipPrefValue > 100 is what is executed if a tip pref is set.

The resetFlags() code has nothing to do with the tip field data entry so is not shown.

If anyone has ANY IDEA why the same code will (seemingly) randomly print out the % symbol, I would really like to hear your thoughts.

Hi @chicago,
Could not run the code, but there are a couple of places you can look,

  1. Is the formatter valid in the context and is it’s format correct?
  2. Is the number recognized correctly?

instead why can you simply not check when leaving the UITextField for tipTextField then add a % at the end than requesting the formatter to add it.

cheers,