0

I have a variable that stores a currency value that I need to use in a calculation, however I can't figure out how to use it within a calculation as the variable has a currency symbol at the start of it (e.g. £2.43, $70.82, €300.21). I have thought about replacing the first character with nothing however this won't work because some currencies use more than one character and some have the symbol after the value.

Does anyone know how I can remove all characters with the exception of the value itself? Or another way to get the variable into a calculation? (I am using Swift by the way)

EDIT:

I've been trying NSDecimalNumber to convert the string however I am getting the error 'NSNumber is not convertible to NSDecimalNumber'. This is the code I have:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    resetButtonNav.enabled = false

    self.navigationController.navigationBar.barTintColor = UIColor.newBlueColor()
    self.tabBarController.tabBar.selectedImageTintColor = UIColor.newBlueColor()
    UINavigationBar.appearance().barTintColor = UIColor.newBlueColor()
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()

    textField.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
    self.view.addSubview(textField)

    currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    currencyFormatter.currencyCode = NSLocale.currentLocale().displayNameForKey(NSLocaleCurrencySymbol, value: NSLocaleCurrencyCode)
}

func textFieldDidChange(textField: UITextField) {
    var text = textField.text.stringByReplacingOccurrencesOfString(currencyFormatter.currencySymbol, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.groupingSeparator, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.decimalSeparator, withString: "")
    textField.text = currencyFormatter.stringFromNumber((text as NSString).doubleValue / 100.0)
}

@IBAction func enterBudgetButton(sender : AnyObject) {
    finalUserBudget = textField.text
    budgetName = budgetNameText.text

    var currencyFormatterTest:NSNumberFormatter
    currencyFormatterTest.formatterBehavior = NSNumberFormatterBehavior.Behavior10_4
    currencyFormatterTest.generatesDecimalNumbers = true
    currencyFormatterTest.numberStyle = NSNumberFormatterStyle.CurrencyStyle

    var finalUserBudgetFormatted: NSDecimalNumber = currencyFormatter.numberFromString(finalUserBudget)
    }
user3746428
  • 11,047
  • 20
  • 81
  • 137
  • 2
    You want to use [`NSNumberFormatter`](http://stackoverflow.com/questions/3560869/using-nsnumberformatter-to-get-a-decimal-value-from-an-international-currency-st) to turn the string into a `NS(Decimal)Number` – Matthias Bauch Jul 28 '14 at 20:32

1 Answers1

1

You can use this code from your question:

var text = textField.text.stringByReplacingOccurrencesOfString(currencyFormatter.currencySymbol, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.groupingSeparator, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.decimalSeparator, withString: "")
textField.text = currencyFormatter.stringFromNumber((text as NSString).doubleValue / 100.0)

to make this:

func currencyStringAsDouble(currencyString: String) -> Double {
    let cleanedString = currencyString.stringByReplacingOccurrencesOfString(currencyFormatter.currencySymbol, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.groupingSeparator, withString: "").stringByReplacingOccurrencesOfString(currencyFormatter.decimalSeparator, withString: "")
    let currencyDouble = (text as NSString).doubleValue / 100.0
    return currencyDouble;
}
Logan
  • 52,262
  • 20
  • 99
  • 128
  • Sorry, are you suggesting I use that function in addition to the other function? – user3746428 Jul 29 '14 at 00:59
  • It depends. How are you storing your values? Are you storing price objects as strings? If so, you can use this to convert them back to doubles. I would suggest just storing them as doubles to avoid problems in the future, in which case this can be used in place of your current implementation as well as when you want to convert some text to a double for storage. – Logan Jul 29 '14 at 01:14
  • Ah, that's perfect! I decided just to store them as doubles like you suggesting and it's working great. Thanks again! – user3746428 Jul 29 '14 at 01:41