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:
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 )
}