Currency converter in Swift 4.2

Mariann Mateka
3 min readNov 19, 2018

--

How to convert UITextField input to display currency and how to convert back to double format for calculation.

Do you want to take your app to the next level by displaying your data in a correct way when you are working with currencies? This post explains how to convert the user’s input as well as the calculated results from string to currency output.

Let’s get started!

First you will need two helper functions: one for convert String to Currency, the other for convert Currency to Double, so you can use the data for calculation!

func convertDoubleToCurrency(amount: Double) -> String{
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
numberFormatter.locale = Locale.current
return numberFormatter.string(from: NSNumber(value: amount))!
}
func convertCurrencyToDouble(input: String) -> Double? {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
numberFormatter.locale = Locale.current
return numberFormatter.number(from: input)?.doubleValue
}

The NumberFormatter class comes handy, and with the numberStyle instance property you can set the type to currency! With the locale instance property the currency type can be declared. If you use Locale.current your app will display the currency according to your current location. But what if you want a specific currency regardless of your location? Here is the formula for fixed currency:

let usLocale = Locale(identifier: "en_US")
let frenchLocale = Locale(identifier: "fr_FR")
let germanLocale = Locale(identifier: "de_DE")
let hungarianLocale = Locale(identifier: "hu_HU")
let hungarianLocale = Locale(identifier: "it_IT")

The convertDoubleToCurrency function is enough to display calculated data in for example the UILabel control:

calculatedLabel.text = convertDoubleToCurrency(amount: calculatedAmount)

Now learn how to display user’s input in currency:

var typedValue: Int = 0
func typedValueToCurrency() -> String? {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
numberFormatter.locale = Locale.current
let amount = Double(typedValue/100) +
Double(typedValue%100)/100
return numberFormatter.string(from: NSNumber(value: amount))!
}
func textField(_ textFieldToChange: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textFieldToChange == typedValueTextField{
if let digit = Int(string) {
typedValue = typedValue * 10 + digit
totalAmount.text = typedValueToCurrency()
}
if string == "" {
typedValue = typedValue /10
totalAmount.text = typedValueToCurrency()
}
} else if textFieldToChange == textFieldnotInCurrency {
return true
}
return false
}

So what is happening inside the functions? Or where should you start? First in the ViewController declare a variable, in this case it is typedValue integer and assigned to 0. Don’t forget to assign a delegate to your textField in order to be able to make changes!

Next talk about typedValueToCurrency function, which is a helper function to format the currency and the value to how is going to be displayed: $ 0.00

The textField function is actually a built-in instance method that asks the delegate if the specified text should be changed. This is why it is important to assign delegate to the textField as I mentioned above. This method returns a Boolean: true if the specified text range should be replaced; otherwise, false to keep the old text.

In this example other non-currency textField were taken into account as well. If you don’t want to make changes in a given textField, simply return true:

else if textFieldToChange == textFieldnotInCurrency {return true}

Back to the textField that you want to modify displaying the input in currency:

if string == “”

condition will apply if you hit backspace, otherwise

if let digit = Int(string)

condition determine the format of the character typed.

You are all set! Go ahead and try it out!

--

--