C语言中用printf()怎么保留3位小数,末位四舍五入输出一个浮点数?

来源:百度知道 编辑:UC知道 时间:2024/07/04 13:23:47
RT
Sample Input
3.55555
Sample Output
3.556

printf("%.3f",变量);

试试看,貌似是这样……

输出语句可以这样写: printf("%.3f\n",a); 假设a是你要输出的变量。其中".3f"就是指限定输出3位小数,并且C语言会自动进行末位的四舍五入。

printf("%.3f\n",变量名);

printf("%4,3f\n",变量)