I have one text field. My text field text is a number with currencyFormatter. My text field text is like "100,000". Now I want sum this textField text with 1000 but I can't. I Use currencyFormatter() Function like that self.textField.text = 100000.currencyFormatter()
My function is:
extension Int {
func currencyFormatter() -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 0
if let result = formatter.string(from: NSNumber(value: self))
{
return result;
}
return String(self);
} }
func sum(){
let firstValue = Int(txtAmount.text!)
let secondValue = 1000
if firstValue != nil {
let outputValue = Int(firstValue! - secondValue)
self.txtAmount.text = "\(outputValue)"
}
else{
self.txtAmount.text = "\(firstValue!)"
}
}
NumberFormatterto parse the number first.