This is the second part of the lesson that I posted previously. This post will focus on creating the fill from right cash text field. As a reminder, the fill from right cash field always displays the dollar symbol, something in the ones place (0 on initial load) and fills in the text from the right side. So on first load the text field shows $0.00 then if you were to enter 5 it would show $0.05. If you entered 1-5 then it would show $5.15. So let's dig in. To start with I set the keyboard type to Number Pad like I did with the zip code field in my previous post. Here is my first attempt at this problem. I'm not too far off. With that code there is a bug in how the new value is being calculated. As it stands if you have $5.15 and then enter 2 you will get $5.17 instead of the desired $51.52. On a side note does anybody know of an easier way to problem math operations on NSDecimalNumbers? Personally, I don't like that syntax. I think it is more difficult to read than this. But as you can see it generates a compiler error so I switched to using the built in methods off of NSDecimalNumber. I don't know if it is because I'm new to the language (very good chance) or because of how the language is designed using the methods off of NSDecimalNumber doesn't read as clearly to me as just using the symbols like I would in C#. I made a few changes and now everything seems to be working. If this were some production code I would certainly want to add unit testing around this, but it is good enough for this. I moved the logic to divide the new number of 100 onto the line 27 where I am creating the additionalValue variable as a NSDecimalNumber. Then on line 30 I take the current value and multiple it by 10 to free up the last digit and finally on line 31 I add the new additional value, created on line 27, to the current value. I hope that makes sense to you. If not let me and I'll try to clarify it. Thanks for reading and happy coding.
Sean Wernimont The Blind Squirrel Copyright 2015-2020
|
AuthorWelcome to The Blind Squirrel (because even a blind squirrel occasionally finds a nut). I'm a full-stack web and mobile developer that writes about tips and tricks that I've learned in Swift, C#, Azure, F# and more. Archives
April 2018
Categories
All
|