gpt4 book ai didi

c - %3.1f 在 C 中做什么?

转载 作者:太空宇宙 更新时间:2023-11-04 05:25:49 25 4
gpt4 key购买 nike

我正在阅读一本关于 C 的旧学校(1990 年)书籍,其中有一个数字格式,循环如下:

... 
...
printf("%3.1f %15.10f\n",x, x*x+x+1/x);

书上说的

%m.kf Converts a value of type float (or double) to m decimal positions with k digits after the period.

在玩弄了这个之后,我仍然完全感到困惑。

我玩过这个。 http://cstub.com/196059842/

#include <stdio.h>
int main( int argc, const char* argv[] )
{
double f = 1.55568;
printf("%10.12f",f);
}

1.555680824083

最佳答案

检查这个例子:

double f = 1.234;
printf("%10.2f",f); // 10 positions wide, 2 decimals at most

输出:

      1.23 <- print only 2 decimal digits
^^^^^^^^^^
||||||||||
0123456789 <- positions

当您想要输出统计数据并且希望一切都很好地对齐时,这样的代码有点常见


在你的例子中:

double f = 1.55568;
printf("%10.12f",f);

你说在句点后最多打印 12 位数字。当然,由于其表示形式,该数字的位数比您在分配值时所写的位数多。

您还说打印的内容最少应该有10个位置,这意味着如果数字不够大,将在数字之前放置空格以满足要求。

关于c - %3.1f 在 C 中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28129598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com