格式化UITextField作为货币

时间:2014-01-12 18:25:24

标签: ios objective-c uitextfield

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

我想在用户输入数字时创建类似掩码的内容来格式化UITextField。例如:

9变为$ 0,09 99变为$ 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;
}

2 个答案:

答案 0 :(得分:2)

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

<强>夫特:

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 = ""
        }
}

答案 1 :(得分:0)

您可以通过以下方式将integer或double / float值转换为字符串:

NSString *str = [@(myInt) stringValue];
NSString *str1 = [[NSNumber numberWithFloat:myFloat] stringValue];

之后,您可以通过多种方式将字符串转换为NSDecimalNumber。像:

NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:@"100.1"];
NSLog(@"%@", number);
NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:@"100.1" locale:NSLocale.currentLocale];
NSLog(@"%@",num);
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setLocale:[NSLocale currentLocale]];
NSLog(@"%@", [formatter stringFromNumber:number]);

NSNumberFormatter将帮助您进行正确的格式化。请阅读NSDecimalNumberNSNumberFormatter的apple页面。 NSNumberFormatter包含一个名为&#34;配置货币格式&#34;的部分。我没有尝试过,但它似乎可以帮助你。

请告诉我这是否有帮助.. :)