Number padding with decimal
You may see some personal fincial app , you need to put the number like calculator.
Example : First it show 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 first number is zero or not.
- If zero , remove it.
- Move the decimal posiotion.
A bit complicate. Actually it’s not hard like that.
Very simple way is
- Append the string at the end.
- remove .
- dvided by 100
That all. 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];