printf() 中的 e 格式和精度修饰符

e format in printf() and precision modifiers

你能解释一下为什么吗

printf("%2.2e", 1201.0);

给出结果 1.20e+03 而不仅仅是 12.01e2?

我的想法:默认号码是1201.0,说明符告诉数字后面应该有2个数字。

怎么了?

根据Wikipedia

In normalized scientific notation, the exponent b is chosen so that the absolute value of a remains at least one but less than ten (1 ≤ |a| < 10). Thus 350 is written as 3.5×102. This form allows easy comparison of numbers, as the exponent b gives the number's order of magnitude. In normalized notation, the exponent b is negative for a number with absolute value between 0 and 1 (e.g. 0.5 is written as 5×10−1). The 10 and exponent are often omitted when the exponent is 0.

Normalized scientific form is the typical form of expression of large numbers in many fields, unless an unnormalised form, such as engineering notation, is desired. Normalized scientific notation is often called exponential notation—although the latter term is more general and also applies when a is not restricted to the range 1 to 10 (as in engineering notation for instance) and to bases other than 10 (as in 3.15× 220).

%e 格式使用科学记数法,即小数点分隔符前一位数字和缩放指数。您不能使用此格式设置小数分隔符 的数字。

这就是科学记数法的定义方式。您期望的结果是一个非常奇怪的符号。我不认为你可以用 printf 得到它。

格式说明符中点之前的数字定义了结果子字符串的最小宽度。尝试 %20.2e 看看这意味着什么。

"%2.2e" 中的第一个 2 是要打印的 最小 字符宽度。 1.20e+03 是 8 个字符,多于 2 个。

e 指示打印数字:(符号),1 位数字,'.',后跟一些数字和指数。

"%2.2e"中的第2个2是打印小数点后的位数。如果未提供第二个值,则使用 6。