So I am working on a tip calculator as my first self guided iOS app and I want to be able to output to the UILabel that shows the bill with currency format. As an example, user presses 1, label shows $0.01, user then enters 2, label shows $0.12, user enters 3, label shows $1.23 and so on. Currently I am getting input and updating the display from UIButton’s that have the numbers on them like this:
NSString *digit = sender.currentTitle;
self.display.text = [self.display.text stringByAppendingString:digit];
I have read several questions on here about number formatting and tried the solutions but cannot get anything to work, but most of the solutions were for UITextFields, so I am thinking perhaps there is a problem with how I am getting input? Any help would be greatly appreciated.
*to clarify, what I am going for is speed. I have a decimal button and have the display with the $ sign, but I want to get rid of the decimal button so all the user has to do is punch in the numbers and not even worry about putting the decimal.
Here’s an idea, why not store the number in cents/pennies – i.e the smallest possible value for a currency. That way if the user presses another number you can simply multiply the existing number by 10 and add the new number.
An example:
1
12
= 123
Now to display this, all you have to do is divide the number by 100 (or whatever is suitable for the given currency) and use the result in a string with format %0.01f