Format a UITextField for currency

2020-03-31 02:21发布

问题:

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 would like to create something like a mask to format the UITextField as the user enter the numbers. For example:

9 becomes $0,09 99 becomes $0,99 999 becomes $999,99

The code below works great, but as I'm using integer and float values the app will eventually display wrong values afeter a certain point. For example:

999999999 becomes 100000000

That happens because flot and integer aren't precise enough and NSDEcimalNumber should be used. The point is that I can't figure out how to replace my integers and float values to NSDecimalNumber ones.

Does anyone could give me a hand to solve this? I spend some time searching the web for a solution but didn't find that suits my needs and tons of people with the same problem.

Heres the code:

-(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;
}

回答1:

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.. :)



回答2:

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