问题描述
在iOS 8的NSNumberForamtter
上同时使用maximumFractionDigits
和maximumSignificantDigits
时,这是一个错误吗?
Is this a bug when using maximumFractionDigits
and maximumSignificantDigits
together on NSNumberForamtter
on iOS 8?
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.maximumFractionDigits = 2;
formatter.maximumSignificantDigits = 3;
NSLog(@"%@", [formatter stringFromNumber:@(0.3333)]); // output 0.333 expected 0.33
如果我仅使用maximumFractionDigits
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.maximumFractionDigits = 2;
NSLog(@"%@", [formatter stringFromNumber:@(0.3333)]); // output expected .33
推荐答案
这不是错误.从文档中:
It isn't a bug. From the docs:
将数字格式化程序配置为使用有效数字时,它将忽略用于设置整数或小数位的任何最小值或最大值.
When a number formatter is configured to use significant digits, it ignores any minimum or maximum values used to set integer or fraction digits.
注意:如果maximumSignificantDigits
或minimumSignificantDigits
设置为任何值,则usesSignificantDigits
将自动设置为true.这与maximumSignificantDigits
和minimumSignificantDigits
的文档相反:
Note: If either maximumSignificantDigits
or minimumSignificantDigits
is set to any value, then usesSignificantDigits
is automatically set to true. This contradicts the docs for maximumSignificantDigits
and minimumSignificantDigits
which says:
您无需将usesSignificantDigits
设置为true,它将自身设置为true.
You don't need to set usesSignificantDigits
to true, it will set itself to true.
这篇关于NSNumberFormatter maximumFractionDigits和maximumSignificantDigits错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!