iOS:移除NSDecimalNumber中的千位分隔符以进行编辑 [英] iOS: Remove thousand separator in NSDecimalNumber for edit

查看:97
本文介绍了iOS:移除NSDecimalNumber中的千位分隔符以进行编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将十进制数字存储为十进制.然后根据用户的语言环境显示它们:

I have decimal numbers stored in my database as decimal. And I display them according to the user's locale:

- (NSString *) getLocalizedCurrencyStringWithDigits
{
    NSNumberFormatter *numberFormatter =[[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setLocale:[NSLocale currentLocale]];
    [numberFormatter setMinimumFractionDigits:2];
    [numberFormatter setMaximumFractionDigits:2];

    NSString *numberString = [numberFormatter stringFromNumber:self];

    return numberString;
}

然后,我在可编辑的文本字段中显示这些字符串.因此,如果用户开始编辑该字段,我想删除千位分隔符.否则(在我的国家/地区),我输入1'000.55,然后它不识别'"并仅保存1.这是我解析文本字段的功能,在视图控制器中,此确切的返回值将保存到数据库:

And I display these strings in textfields, that are editable. So if a user starts to edit the field, I want to remove the thousand separator. Otherwise (in my country) I enter 1'000.55 and it then doesn't recognize the "'" and saves just a 1. Here is my function to parse the textfields and in the view controllers this exact return value will be saved to the database:

+ (NSDecimalNumber *) getUnLocalizedDecimalNumberWithString:(NSString *)currencyString
{
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setLocale:[NSLocale currentLocale]];
    [numberFormatter setMinimumFractionDigits:2];
    [numberFormatter setMaximumFractionDigits:2];

    BOOL isDecimal = [numberFormatter numberFromString:currencyString] != nil;
    if(isDecimal){
        return [NSDecimalNumber decimalNumberWithString:currencyString locale:[NSLocale currentLocale]];
    } else {
        return [NSDecimalNumber zero];
    }

但是我没有让它起作用……实际上,最好将带有'"和不带有'"的数字正确保存,但是我不起作用:/

However I don't get it to work...actually it would be best if numbers with"'" and without would be saved correctly but I don't get it to work :/

编辑(我的解决方案):像这样工作了:

- (NSString *) getLocalizedCurrencyStringWithDigits:(int)digits
{
    NSNumberFormatter *numberFormatter =[[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setLocale:[NSLocale currentLocale]];
    [numberFormatter setMinimumFractionDigits:digits];
    [numberFormatter setMaximumFractionDigits:digits];

    NSString *numberString = [numberFormatter stringFromNumber:self];

    return numberString;
}

+ (NSDecimalNumber *) getUnLocalizedDecimalNumberWithString:(NSString *)currencyString
{
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setLocale:[NSLocale currentLocale]];
    [numberFormatter setMinimumFractionDigits:2];
    [numberFormatter setMaximumFractionDigits:2];

    NSNumber *formatedCurrency = [numberFormatter numberFromString:currencyString];

    BOOL isDecimal = formatedCurrency != nil;
    if(isDecimal){
        return [NSDecimalNumber decimalNumberWithDecimal:[formatedCurrency decimalValue]];
    } else {
        return [NSDecimalNumber zero];
    }
}

+(NSDecimalNumber *)getUnLocalizedDecimalNumberWithString:(NSString *)currencyString

+(NSDecimalNumber *) getUnLocalizedDecimalNumberWithString:(NSString *)currencyString

现在可以正确解释每个字符串.1'000、1000和1000.00现在都是1000了:-)

interprets now every string correctly. 1'000 and 1000 and 1000.00 are all 1000 now :-)

推荐答案

我认为最简单,最干净的解决方案是:

The simplest and most clean solution I believe is this:

NSLocale *locale = [NSLocale currentLocale];
NSString *thousandSeparator = [locale objectForKey:NSLocaleGroupingSeparator];
NSString *result = [decimalStringWithThousandSeperator stringByReplacingOccurrencesOfString:thousandSeparator withString:@""];

这篇关于iOS:移除NSDecimalNumber中的千位分隔符以进行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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