格式化UITextField作为货币 [英] Format a UITextField for currency

查看:112
本文介绍了格式化UITextField作为货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

I would like to create something like a mask to format the UITextField as the user enter the numbers. For example:

9变为$ 0,09
99变为$ 0.99
999变为$ 999,99

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变为100000000

999999999 becomes 100000000

这是因为flot和integer不够精确且NSDEcimalNumber应该使用。关键是我无法弄清楚如何将我的整数和浮动值替换为NSDecimalNumber。

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.

下面是代码:

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


推荐答案

您可以转换整数或双精度/ float value to string:

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

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

之后你可以通过多种方式将字符串转换为NSDecimalNumber。喜欢:

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将帮助您进行正确的格式化。请阅读apple页面以获取 NSDecimalNumber NSNumberFormatter
NSNumberFormatter包含一个名为配置货币格式的部分。我没有尝试过,但它似乎可以帮到你。

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

这篇关于格式化UITextField作为货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆