Number Padding with Decimal

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

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

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

The very simple way is

  1. Append the string at the end.
  2. remove.
  3. 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];
Gravatar Image
My name is Than Dana but I’d like to be called Dana. I’m 28 years old and a single man. I’m from Toungoo, Bago division. I live in Yangon for my job and work as Digital Marketing Executive @ Win Mobile World Co., Ltd. I stand as a professional in IT fields and have been sharing, inventing and advancing tech developments since about 2008.