Format a UITextField for currency

前端 未结 2 663
梦谈多话
梦谈多话 2021-01-16 10:34

I have a UITextField on my aplication that receives only numeric input from the user. This numeric input represents currency and have the default value of 0.00.

I wo

相关标签:
2条回答
  • 2021-01-16 11:32

    You can convert integer or double/float value to string by following:

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

    After that you can convert string to NSDecimalNumber by many ways. Like:

    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 will help you with the right formatting. Please read apples page for NSDecimalNumber and NSNumberFormatter. NSNumberFormatter contains a section named"Configuring the Format of Currency". I didn't try it, but it seems something that can help you.

    Let me know if this helps.. :)

    0 讨论(0)
  • 2021-01-16 11:33

    Implement UITextFieldDelegate and add next methods:

    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 = ""
            }
    }
    
    0 讨论(0)
提交回复
热议问题