问题描述
我将十进制数字存储为十进制.然后根据用户的语言环境显示它们:
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];
}
}
现在可以正确解释每个字符串.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中的千位分隔符以进行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!