views:

205

answers:

1

I am presenting the user with a field that displays the number keyboard and only want them to be able to enter numbers--no decimals. Since the field is a currency field, however, I want the number to always display with two decimal places. You've probably seen this type of an interface at an ATM.

I've looked at the NSNumberFormatter docs and general formatting using [NSString stringWithFormat], but it's not clear to me a good way to do this. I am listening on the field with a selector for UIControlEventEditingChanged events. It's in that selector I want to do the formatting. When I use [NSString stringWithFormat: @"%.2f", amount], it always places amount to the left of the decimal. I want it to always be the fraction portion until the third digit is entered, so if the user types 32, the actual amount is .32. If the user enters 173, the value is actually 1.73. If the user enters 10047, the actual value should be 100.47 etc.

Any suggestions?

Thanks.

+2  A: 

Treat the amount they're entering as cents instead of dollars. Divide it by 100 before formatting.

John Kugelman
So that's obvious now. I should've stayed in bed today, I think. Head colds suck. ;-) Thanks for the answer.
Matt Long
I wouldn't use floating point, keep it as ints: [NSString stringWithFormat: @"%d.%02d", amount/100, amount%100]
progrmr