, currencyFormatter? !, intialised? , Swift iOS:
var myTextField = UITextField()
var currencyFormatter : NSNumberFormatter!
func myFunc(textField: UITextField) {
var text : String = textField.text
text = textField.text
.stringByReplacingOccurrencesOfString(currencyFormatter.currencySymbol!, withString: "")
.stringByReplacingOccurrencesOfString(currencyFormatter.groupingSeparator, withString: "")
.stringByReplacingOccurrencesOfString(currencyFormatter.decimalSeparator!, withString: "")
.stringByReplacingOccurrencesOfString(" ", withString: "")
println("original: \(textField.text), after replacing: \(text)")
textField.text = currencyFormatter.stringFromNumber((text as NSString).doubleValue / 100.0)
println("new: \(textField.text)")
}
myTextField.text = "$123.000,00"
myFunc(myTextField)
myTextField.text
, currencyFormatter, if , . , , , else, , currencyFormatter nil.
, , , , currencyFormatter.currencySymbol! currencyFormatter.decimalSeparator!, , nil , text == "".
NSNumberFormatter.numberFromString(), . :
var currencyFormatter = NSNumberFormatter()
currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
if let numberStr = currencyFormatter.stringFromNumber(NSNumber(double: 123567.45)) {
if let number = currencyFormatter.numberFromString(numberStr) {
println("numberStr = \(numberStr) vs number = \( number )")
}
}
numberStr = $123,567.45 vs number = 123567.45 .