简体   繁体   English

什么描述了NSNumberFormatter -maximumSignificantDigits?

[英]What describes NSNumberFormatter -maximumSignificantDigits?

There's no text in the documentation about what that means, but it sounds very important to understand in order to not run into trouble. 文档中没有关于这意味着什么的文本,但为了不遇到麻烦,理解这一点非常重要。 Does someone know what that is all about the "significant digits" of a number? 有人知道数字的“有效数字”是什么吗?

Although the other answer on this question links to a correct explanation of the concept of significant digits in general, NSNumberFormatter 's {uses|minimum|maximum}SignificantDigits properties have nothing to do with precision of calculations. 虽然这个问题的另一个答案与一般有效数字概念的正确解释有关,但NSNumberFormatter{uses|minimum|maximum}SignificantDigits属性与计算精度无关。

The significant digits are the group of digits in a number from the first nonzero digit to the last nonzero digit, inclusive, usually unless trailing zeroes are fractional . 有效数字是从第一个非零数字到最后一个非零数字的数字中的数字组,通常除非尾随零是小数 Restricting output to a specific number of significant digits is useful if a relative (percentage) error is known or desired. 如果已知或需要相对 (百分比)错误,则将输出限制为特定数量的有效数字非常有用。

First of all, the minimumSignificantDigits and maximumSignificantDigits have no effect unless usesSignificantDigits is set to YES . 首先,除非usesSignificantDigits设置为YES否则minimumSignificantDigitsmaximumSignificantDigits无效。 If this is the case, their effect is probably most easily explained using examples. 如果是这种情况,可能最容易用实例解释它们的影响。

Let's take the numbers a = 123.4567 , b = 1.23 , and c = 0.00123 : 我们取数字a = 123.4567b = 1.23c = 0.00123

Assuming minimumSignificantDigits = 0 , 1 or 2 : 假设minimumSignificantDigits = 012

If maximumSignificantDigits = 3 , then a will be formatted as "123", b as "1.23", and c as "0.00123". 如果maximumSignificantDigits = 3 ,则a将格式化为“123”, b格式为“1.23”, c格式为“0.00123”。

If maximumSignificantDigits = 4 , then a will be formatted as "123​ .5 ", b as "1.23" and c as "0.00123". 如果maximumSignificantDigits = 4 ,那么a将被格式化为“123 0.5”, b为“1.23”和c为“0.00123”。

If maximumSignificantDigits = 2 , then a will be formatted as "12​ 0 ", b as "1.2" and c as "0.0012". 如果maximumSignificantDigits = 2 ,则a将被格式化为“12 0”, b为“1.2”和c为“0.0012”。

Assuming minimumSignificantDigits = 4 : 假设minimumSignificantDigits = 4

If maximumSignificantDigits = 4 , then a will be formatted as "123.​ 5 ", b as "1.23​ 0 ", and c as "0.00123​ 0 ". 如果maximumSignificantDigits = 4 ,那么a将被格式化为“123 5”, b为“1.23 0”,并且c为“0.00123 0”。

Note: The 45 conversions occur due to the round-to-nearest mode, as the digit following the 4 in a is 5. 注意:由于舍入到最近的模式,发生45次转换,因为a中的4后面的数字是5。

See here for a nice tutorial on significant digits. 请看这里有关有效数字的精彩教程。 Very simple explanation would be: the number of digits that are used for calculations within your app. 非常简单的解释是:在您的应用程序中用于计算的位数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM