iOS中的NSDecimalNumber错误的小数点分隔符 [英] wrong decimal separator with NSDecimalNumber in iOS

查看:138
本文介绍了iOS中的NSDecimalNumber错误的小数点分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用正确的小数分隔符以下列方式输出十进制数字的描述:

  NSString * strValue = @9.94300; 
NSDecimalNumber * decimalNumber = [NSDecimalNumber decimalNumberWithString:strValue];
NSLocale * locale = [NSLocale currentLocale];
NSLog(@%@,[locale localeIdentifier]);
NSLog(@%@,[decimalNumber descriptionWithLocale:locale]);

输出为:

  de_DE 
9.943

小数点分隔符应为','对于此语言环境而不是。。错误在哪里?如何根据本地输出正确的小数分隔符?

解决方案

@TriPhoenix:是的我正在运行iOS 5. / p>

@Vignesh:非常感谢。如果我将iPhone语言设置为德语,以下代码现在可以使用:

  NSString * strValue = @9.94300; 
NSDecimalNumber * decimalNumber = [NSDecimalNumber decimalNumberWithString:strValue];
NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSLog(@%@,[numberFormatter stringFromNumber:decimalNumber]);

输出为:
9,943



但现在我有另一个问题。如果我将iPhone语言切换回英语,输出仍然是9,943而不是9.943。我做错了吗?


I tried to output the description of a decimal number with the correct decimal separator in the following way:

NSString* strValue = @"9.94300";
NSDecimalNumber* decimalNumber = [NSDecimalNumber decimalNumberWithString: strValue];    
NSLocale* locale = [NSLocale currentLocale];
NSLog(@"%@", [locale localeIdentifier]);
NSLog(@"%@", [decimalNumber descriptionWithLocale: locale] );

The output is:

de_DE
9.943

The decimal separator should be ',' instead of '.' for this locale. Where is the error? How can I output the correct decimal separator depending on the local?

解决方案

@TriPhoenix: Yes I'm running iOS 5.

@Vignesh: Thank you very much. The following code works now if I set the iPhone language to German:

NSString* strValue = @"9.94300";
NSDecimalNumber* decimalNumber = [NSDecimalNumber decimalNumberWithString: strValue];    
NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior: NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
NSLog(@"%@", [numberFormatter stringFromNumber: decimalNumber] );

The output is: 9,943

But now I have another problem. If I switch the iPhone language back to English the output is still 9,943 instead of 9.943. Do I make something wrong?

这篇关于iOS中的NSDecimalNumber错误的小数点分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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