Use keypad to enter in currency value in desired format

♀尐吖头ヾ 提交于 2019-12-02 06:07:49

问题


I want to have a screen that starts off with "$0.00" (or other symbol based on locale) that I can enter values into and it will update one at a time. (type 1234, returns $0.01, $0.12, $1.23, $12.34). I know how to do this with the digits entered into a text field and with the click of a button change the formatting either in the text field or into a new label. What I want, though, is to do it without the extra button tap and use a label, not a textField.

float moneyEarnedFloat = [revenueTextField.text floatValue]/100.0f;
NSNumber *moneyEarned = [NSNumber numberWithFloat:moneyEarnedFloat];
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

revenueTextField.text = [currencyFormatter stringFromNumber:moneyEarned];

An example would be mint.com's Add Transaction screen.


回答1:


Try this:

NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
int currencyScale = [currencyFormatter maximumFractionDigits];

TextFieldDidChange:

[mytextField addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];

Then do:

    - (void)textFieldDidChange {
myLabel.text = mytextField.text;
}


来源:https://stackoverflow.com/questions/8305669/use-keypad-to-enter-in-currency-value-in-desired-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!