Format currency in textfield in Swift on input

前端 未结 7 1783
一生所求
一生所求 2021-02-08 07:52

I am trying to format currency input in a textfield in Swift as the user inputs it.

So far, I can only format it successfully when the user finishes inputting:



        
7条回答
  •  梦谈多话
    2021-02-08 08:41

    I worked out a normal currency format ( eg 1 is as $1.00, 88885 is as $8,8885.00 and 7555.8569 as $7,555.86.

    @IBAction func lostpropertyclicked(sender: AnyObject) {
        var currentString = ""
        currentString = amountTF.text
        formatCurrency(string: currentString)
    }
    
    func formatCurrency(#string: String) {
        println("format \(string)")
        let formatter = NSNumberFormatter()
        formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
        formatter.locale = NSLocale(localeIdentifier: "en_US")
        var numberFromField = (NSString(string: currentString).doubleValue)
        currentString = formatter.stringFromNumber(numberFromField)!
        println(currentString )
    }
    

提交回复
热议问题