使用键盘以所需格式输入货币值 [英] Use keypad to enter in currency value in desired format

查看:100
本文介绍了使用键盘以所需格式输入货币值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个以 $ 0.00(或其他基于语言环境的符号)开头的屏幕,我可以在其中输入值,并且每次更新一个。 (类型1234,返回$ 0.01,$ 0.12,$ 1.23,$ 12.34)。
我知道如何使用输入文本字段中的数字并单击按钮来更改文本字段或新标签中的格式。不过,我想要的是在没有额外按钮点击的情况下执行此操作,并使用标签,而不是textField。

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];

示例是mint.com的添加交易记录屏幕。

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

推荐答案

尝试一下:

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

TextFieldDidChange:

TextFieldDidChange:

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

然后执行:

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

这篇关于使用键盘以所需格式输入货币值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆