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

  1. Append the string at the end
  2. Check first number is zero or not.
  3. If zero , remove it.
  4. Move the decimal posiotion.

A bit complicate. Actually it’s not hard like that.

Very simple way is

  1. Append the string at the end.
  2. remove .
  3. 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];
comments powered byDisqus