You may see some personal financial app, you need to put the number like a calculator.
Example: First it shows 0.00
You enter 1
it will be like 0.01. And then press 5, it will be 0.15. And then press 3, it will be 1.53.
So, how to write this one?
Normal thinking way it, you need to check
- Append the string at the end
- Check the first number is zero or not.
- If zero, remove it.
- Move the decimal position.
A bit complicate. Actually it’s not hard like that.
The very simple way is
- Append the string at the end.
- divided by 100
That all. A very simple step.
Example with objective-c, it will be like
NSString* str = [NSString stringWithFormat:@"%@%@", txtAmount.text,string]; str = [str stringByReplacingOccurrencesOfString:@"." withString:@""]; txtAmount.text = [@"" stringByAppendingFormat:@"%0.2f", [str doubleValue]/100.0];