Python 小数精度

Precision in Python decimal

为什么我在第二行设置decimal.getcontext().prec = 2时看到decimal.getcontext().prec = 1

decimal.getcontext().prec = 2
decimal.getcontext().rounding = 'ROUND_HALF_DOWN'

print(decimal.Decimal(1) / decimal.Decimal(7))
print(decimal.Decimal(9949) / decimal.Decimal(1000))

# 0.14
# 9.9

在这种情况下,数字 9.9 的精度为 2。

来自 Significant figures 上的维基百科文章:

精度(a.k.a.有效数字或显着数字)定义为:

digits that carry meaningful contributions to its measurement resolution. This includes all digits except:

  • All leading zeros. For example, "013" has two significant figures: 1 and 3
  • Trailing zeros when they are merely placeholders to indicate the scale of the number (exact rules are explained at identifying significant figures)
  • Spurious digits introduced, for example, by calculations carried out to greater precision than that of the original data, or measurements reported to a greater precision than the equipment supports.

Significant figures rules explained

The rules for identifying significant figures when writing or interpreting numbers are as follows:

  • All non-zero digits are considered significant. For example, 91 has two significant figures (9 and 1), while 123.45 has five significant figures (1, 2, 3, 4 and 5).
  • Zeros appearing anywhere between two significant figures are significant: 101.1203 has seven significant figures: 1, 0, 1, 1, 2, 0 and 3.
  • Zeros to the left of the significant figures (leading zeros) are not significant. For example, 0.00052 has two significant figures: 5 and 2.
  • Zeros to the right of the non-zero digits (trailing zeros) are significant if they are to the right of the decimal point as these are only necessary to indicate precision. However trailing zeros in the ones place or higher may or may not be significant, depending on the precision of the measurement. Thus 1.20 and 0.0980 have three significant figures whereas 45,600 may have 3, 4 or 5 significant figures. Note that 120.00 would have five significant figures - the zero to the left of the decimal is significant because it is between two significant figures (the 2 and the zeros to the right of the decimal point).

要将小数四舍五入到小数点后两位数,您可以执行以下操作:

decimal.getcontext().prec = 5
print(f"{decimal.Decimal(9949) / decimal.Decimal(1000):.2f}")

输出

9.95