努力与货币在可可粉 [英] Struggling with currency in Cocoa
问题描述
我想做一些我认为相当简单的事情:让用户输入一个美元金额,将该金额存储在NSNumber(NSDecimalNumber?)中,然后在稍后的时间再次显示格式化为货币的金额。
我的麻烦不在于setNumberStyle:NSNumberFormatterCurrencyStyle,并将浮动显示为货币。麻烦更多是如何说明numberFormatter与这个UITextField工作。我可以找到几个例子。 来自十一月的此主题和这一个给我一些想法,但给我更多的问题。
我使用UIKeyboardTypeNumberPad键盘,并且理解我应该在显示时显示$ 0.00(或任何本地货币格式),因为用户输入数字以移动小数位:
- 从显示$ 0.00开始
- 点按2键:display $ 0.02
- 点按5键:display $ 0.25
- 点按4键:display $ 2.54
- 点按3键:display $ 25.43
然后[numberFormatter numberFromString:textField.text]应该给我一个值,我可以存储在我的NSNumber变量。
很抱歉,我还在努力:这是否真的是最好的/最简单的方法?如果是这样,那么也许有人可以帮助我的实现?我觉得UITextField可能需要一个委托响应每个按键,但不知道什么,在哪里和如何实现它?任何示例代码?我非常感谢! 因此,我正在查找NSFormatter的 stringForObjectValue:。和我可以找到的最接近的事情是什么benzado推荐: UITextViewTextDidChangeNotification 。真的很难在其中任何一个查找示例代码...所以让我知道,如果你知道在哪里看看。
我的解决方案:
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange :(NSRange)range
replacementString:(NSString *)string
{
//清除所有不是数字的字符
//(如货币符号或分隔符)
NSString * cleanCentString = [[textField.text
componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:@];
//解析最终整数值
NSInteger centAmount = cleanCentString.integerValue;
//检查用户输入
if(string.length> 0)
{
//添加的数字
centAmount = centAmount * 10 + string.integerValue;
}
else
{
//删除数字
centAmount = centAmount / 10;
}
//更新调用金额值
[_amount release];
_amount = [[NSNumber alloc] initWithFloat:(float)centAmount / 100.0f];
//使用货币符号到文本字段的写入量
NSNumberFormatter * _currencyFormatter = [[NSNumberFormatter alloc] init];
[_currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[_currencyFormatter setCurrencyCode:@USD];
[_currencyFormatter setNegativeFormat:@ - ¤#,## 0.00];
textField.text = [_currencyFormatter stringFromNumber:_amount];
[_currencyFormatter release]
//因为我们已经将我们的更改写入文本字段
//我们不想再更改文本字段
return NO;
}
I'm trying to do something I'd think would be fairly simple: Let a user input a dollar amount, store that amount in an NSNumber (NSDecimalNumber?), then display that amount formatted as currency again at some later time.
My trouble is not so much with the setNumberStyle:NSNumberFormatterCurrencyStyle and displaying floats as currency. The trouble is more with how said numberFormatter works with this UITextField. I can find few examples. This thread from November and this one give me some ideas but leaves me with more questions.
I am using the UIKeyboardTypeNumberPad keyboard and understand that I should probably show $0.00 (or whatever local currency format is) in the field upon display then as a user enters numerals to shift the decimal place along:
- Begin with display $0.00
- Tap 2 key: display $0.02
- Tap 5 key: display $0.25
- Tap 4 key: display $2.54
- Tap 3 key: display $25.43
Then [numberFormatter numberFromString:textField.text] should give me a value I can store in my NSNumber variable.
Sadly I'm still struggling: Is this really the best/easiest way? If so then maybe someone can help me with the implementation? I feel UITextField may need a delegate responding to every keypress but not sure what, where and how to implement it?! Any sample code? I'd greatly appreciate it! I've searched high and low...
Edit1: So I'm looking into NSFormatter's stringForObjectValue: and the closest thing I can find to what benzado recommends: UITextViewTextDidChangeNotification. Having really tough time finding sample code on either of them...so let me know if you know where to look?
My solution:
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
// Clear all characters that are not numbers
// (like currency symbols or dividers)
NSString *cleanCentString = [[textField.text
componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:@""];
// Parse final integer value
NSInteger centAmount = cleanCentString.integerValue;
// Check the user input
if (string.length > 0)
{
// Digit added
centAmount = centAmount * 10 + string.integerValue;
}
else
{
// Digit deleted
centAmount = centAmount / 10;
}
// Update call amount value
[_amount release];
_amount = [[NSNumber alloc] initWithFloat:(float)centAmount / 100.0f];
// Write amount with currency symbols to the textfield
NSNumberFormatter *_currencyFormatter = [[NSNumberFormatter alloc] init];
[_currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[_currencyFormatter setCurrencyCode:@"USD"];
[_currencyFormatter setNegativeFormat:@"-¤#,##0.00"];
textField.text = [_currencyFormatter stringFromNumber:_amount];
[_currencyFormatter release]
// Since we already wrote our changes to the textfield
// we don't want to change the textfield again
return NO;
}
这篇关于努力与货币在可可粉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!