Format Integer as Two Places Decimal
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 in开发者_高级运维terface 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.
Treat the amount they're entering as cents instead of dollars. Divide it by 100 before formatting.
精彩评论