Currency format input to UITextField in Swift
Tell us how can we improve this post?
So in this video we are going to learn how to add currency textfield like this. So the challenge for this video is when you get to let’s just say 999 million and when you get to 10 figure amount it says please enter amount less than a billion. So basically to set the upper limit to one billion.
And let just say when you add amount and when you remove amount when the amount is zero set the textfield to empty. If you want to know how to fetch today’s exchange rate for all these different currencies from the internet. Then I have totally separate blog post on it. Check out the first link in the description box.
So let’s just dive right into it. I am going to create new X code project. iOS single view application next. I am going to call it currency textfield. Then next. Now I am going to save this on desktop. Alright so first thing I am going to go to the main storyboard. Set the size to iPhone SE. And I am going to add a simple textfield.
here. Make it little bigger. Then set it here. Then I am going to create outlet for this textfield. I am going to call it currency textfield. Connect. Then I am going to close the assistant editor. So I am going to select the textfield and the keyboard type for this textfield is going to be number pad. So I just want to add numbers.
I do not want to add letters or any special characters. Just simply numbers. Also I am going to select textfield and set its alignment to right sided. Because in my opinion it just looks better. Then I am going to go to the view controller. And I am going to write some code in here. And then I will explain it to you.
Alright so here is what I have written. So first I have created an amount integer and set its default value to zero. Then currency textfield dot delegate is equal to self. And I have added the textfield delegate to the view controller. Now for placeholder value I have created function called update amount. So let’s go through it.
So update amount takes in nothing and returns optional string. So I have created number formatter. Set the number style to currency. And basically this amount is going to be because the amount value currently is zero. So even if you divide by 100 or remainder by 100. All of this value is going to be zero.
So from this zero and using this formatter. I am converting this zero into a currency string. And then updating in here right now. Now the reason we have added the delegate is this method. Textfield should change character in range. This is the method we added delegate for. Then the default return is false.
So it returns a bool & by default no matter whatever the character is I am not going to change that character. So by default I am going to return false. So in textfield each time you add a character into a textfield. That new character that you add is this string parameter in this method.
So I check this new character that you added is I convert that string into integer. If it is an integer then whatever the current amount is. So right now the amount is zero. And I am going to multiply by 10. And add that digit and that will be my total amount. So for example the amount is zero. This is zero. Digit is five. And then the amount would be five.
Then I update currency textfield dot text is equal to update amount. Again number formatter I am using the style currency. Now the amount is five. And an amount is an integer. If you divide integer with another integer then the result is going to be an integer. So you don’t get the remainder. So five divided by 100 is going to be zero.
The double value is going to be zero. So five remainder 100. So the remainder is going to be five. Then the five is double. Now I divide this five double by 100. Then the answer is going to be double zero point zero five. So zero plus zero point zero five. The amount is going to be zero point zero five. And I convert this into a string.
And I show this on the textfield. Similarly if this string is equal to empty space. That means I have pressed back space. In that case I divide the five by ten. Since this is an integer and I divide by an integer. The answer is going to be an integer. That is going to be zero. And the textfield I am going to update. This is going to be zero.
So let me just run this app and you will see this app in practice. Alright so as you can see the placeholder is dollar zero point zero zero. It is automatically of type currency and dot zero zero. And so first number I added is six. Zero point zero six, three, five, two, one. As I add each number it is automatically added to the end.
And as I press the backspace. I lose the last number. Now lose the four, now i lose the two , I lose the one. And I see the currency. I have also added touches began so when you click on the white area. The textfield stops editing. Alright so the problem right no is this amount is regular integer. So it cannot hold a very large amount.
For example let’s just say if I add something so its 666 million. And I add billion and I keep adding and see I am adding six but its adding zero. And this app is going to crash. So it cannot hold a very large number. And most people will not do calculation more than billion dollar. Billion in any currency for that matter.
So the challenge is when you add more than a billion in currency it should automatically give warning to the user. And say that you cannot add more than a billion. And when its zero it should automatically turn to this. Right now when you zero it shows this but when I press back space the textfield should be empty.
Alright for the challenge its pretty simple. I am simply going to say if amount is greater than one billion so billion has one, two, three; one two three, one two three. So these are nine zeros and two fractions. If amount is greater than that. Then I am going to show an alert here.
Alright so I have simply created alert here. and the title says please enter amount less than a billion. And its going to show a dismiss button. And when you press the button the handler is nil. So this alert will disappear. Then I am presenting this alert. Then I am setting this textfield dot text to nothing. And reset the amount back to zero.
And if the amount is not bigger than a billion. Then I am just going to update textfield as normal. Let’s say if the amount was five. So when you divide five by ten then the amount is zero. So I am going to check if amount is equal to zero. In that case I am going to set textfield text to nothing. So that’s it. Now let me run this in iPhone 5s.
Alright so as I add amount it takes in. Now its 955 million. And it says please enter amount less than a billion. And let’s just say I add some amount. And I press backspace. And now its zero. And the textfield becomes empty. So if you want to download this X code file. Then just fill out the form below. And you will receive the file in your inbox.
And if you want to fetch today’s exchange rate for all different currencies. Then I have totally separate blog post on it. Check out the first link in the description box. So that’s it for this video. Please like this video, leave a comment. And I will see you in the next one.