gpt4 book ai didi

ios - 为货币格式化 UITextField

转载 作者:行者123 更新时间:2023-11-29 10:49:25 24 4
gpt4 key购买 nike

我的应用程序上有一个 UITextField,它只接收来自用户的数字输入。此数字输入表示货币,默认值为 0.00。

我想创建类似掩码的东西,以便在用户输入数字时格式化 UITextField。例如:

9 变成 $0,0999 变成 0,99 美元999 变成 $999,99

下面的代码运行良好,但由于我使用的是整数和浮点值,应用程序最终会在某个点后显示错误的值。例如:

999999999 变成 100000000

发生这种情况是因为 flot 和 integer 不够精确,应该使用 NSDEcimalNumber。关键是我不知道如何将我的整数和浮点值替换为 NSDecimalNumber 值。

有谁能帮我解决这个问题吗?我花了一些时间在网上搜索解决方案,但没有找到适合我需求的解决方案,而且很多人都有同样的问题。

代码如下:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tag == 1){
NSString *cleanCentString = [[textField.text componentsSeparatedByCharactersInSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
NSInteger centValue= cleanCentString.integerValue;

if (string.length > 0)
{
centValue = centValue * 10 + string.integerValue;
}
else
{
centValue = centValue / 10;
}

NSNumber *formatedValue;
formatedValue = [[NSNumber alloc] initWithFloat:(float)centValue / 100.0f];
NSNumberFormatter *_currencyFormatter = [[NSNumberFormatter alloc] init];
[_currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
textField.text = [_currencyFormatter stringFromNumber:formatedValue];
return NO;
}

if (textField.tag == 2){
// Nothing for now
}

return YES;
}

最佳答案

实现 UITextFieldDelegate 并添加下一个方法:

swift :

let currencySign = "$"

// Adds $ before the text, e.g. "1" -> "$1" and allows "." and ","
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
var value = textField.text

var newString = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
var components = newString.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "1234567890,.").invertedSet)

var decimalString = "".join(components) as NSString
var length = decimalString.length

if length > 0 {
value = "\(currencySign)\(decimalString)"
}
else {
value = ""
}
textField.text = value
}

// Formats final value using current locale, e.g. "130,50" -> "$130", "120.70" -> "$120.70", "5" -> "$5.00"
func textFieldDidEndEditing(textField: UITextField) {
var value = textField.text
var components = value.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "1234567890,.").invertedSet)
var decimalString = "".join(components) as NSString

let number = NSDecimalNumber(string: decimalString, locale:NSLocale.currentLocale())

var formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
formatter.locale = NSLocale.currentLocale()

if let formatedValue = formatter.stringFromNumber(number) {
textField.text = formatedValue
}
else {
textField.text = ""
}
}

关于ios - 为货币格式化 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21078572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com