2

I am trying to format a number from a UITextfield, as its being typed, to a decimal with commas.

I have done so with the following code:

@IBAction func editingDidBegin(sender : AnyObject)
{
    costField.addTarget(self, action: Selector("textFieldDidChange:"), forControlEvents: UIControlEvents.EditingChanged)
}

func textFieldDidChange(theTextField:UITextField) -> Void
{
    var textFieldText = theTextField.text.stringByReplacingOccurrencesOfString(",", withString: " ", options: NSStringCompareOptions.RegularExpressionSearch, range: Range(start: theTextField.text.startIndex, end: theTextField.text.endIndex))
    var formatter:NSNumberFormatter = NSNumberFormatter()
    formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
    var formattedOutput = formatter.stringFromNumber(textFieldText.bridgeToObjectiveC().integerValue)

    costField.text = formattedOutput
}

The problem with this, is after four digits are entered, everything after the comma is deleted. For example if I enter 4000 it formats to 4,000, then if I type another number like 8 it reformats to 48.

Is there another way I can format this, maybe through IB or how can I fix the code?

3 Answers 3

3

Replace the line with:

var textFieldText = theTextField.text.stringByReplacingOccurrencesOfString(",", withString: "", options: NSStringCompareOptions.RegularExpressionSearch, range: Range(start: theTextField.text.startIndex, end: theTextField.text.endIndex))

(I only removed the space between the double quotes).

Fact is, NSNumberFormatter doesn't like the added spaces in the string.

Works fine afterwards.

0

I know I am late to the party but this worked well for me.

var phoneNumber = " 1 (888) 555-5551    "

var strippedPhoneNumber = "".join(phoneNumber.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet))

It takes out the spaces and strips out the non decimal numeric characters.

The end result is "1888555551"

0

I've updated this answer to the newest version of swift. This borrows 90% from the two answers above however, also accounts for nil exception from the textfield when the textfield is cleared.

func textFieldDidChangeCommas(theTextField:UITextField) -> Void
{
    if theTextField.text != nil {

        var textFieldText = theTextField.text!.stringByReplacingOccurrencesOfString(",", withString: "", options: NSStringCompareOptions.RegularExpressionSearch, range: Range(start: theTextField.text!.startIndex, end: theTextField.text!.endIndex))
        var formatter:NSNumberFormatter = NSNumberFormatter()
        formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
        if textFieldText != "" {
            var formattedOutput = formatter.stringFromNumber(Int(textFieldText)!)
            costField.text = formattedOutput
        }
    }
}
2
  • it would be a complete solution if you had added how to call the method to simplify work for those looking for a quick solution without having to look else where, especially beginners.
    – nyxee
    Aug 3, 2017 at 1:07
  • it would be a complete solution if you had added how to call the method to simplify work for those looking for a quick solution without having to look else where, especially beginners. but, both of these worked: textField.addTarget(self, action: #selector(PostPaymentViewController. textFieldDidChangeCommas(_:)) , for: .editingChanged) AND textField.addTarget(self, action: #selector(ViewController. textFieldDidChangeCommas) , for: .editingChanged) . Can someone explain briefly please.
    – nyxee
    Aug 3, 2017 at 3:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.