ractionDigits和maximumSignificant

ractionDigits和maximumSignificant

本文介绍了NSNumberFormatter maximumFractionDigits和maximumSignificantDigits错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 8的NSNumberForamtter上同时使用maximumFractionDigitsmaximumSignificantDigits时,这是一个错误吗?

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.

https://developer.apple.com/documentation /foundation/nsnumberformatter/1417793-usessignificantdigits?language = objc

注意:如果maximumSignificantDigitsminimumSignificantDigits设置为任何值,则usesSignificantDigits将自动设置为true.这与maximumSignificantDigitsminimumSignificantDigits的文档相反:

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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:57