可能是重复问题:
限制浮点精度?
在C语言中,我需要从用户输入的数字中格式化出小数点后2位。
例如:
float x ;
printf("Enter number");
假设用户输入了
4.54234635
我需要在整个程序中打印并进行处理:
4.54
提前感谢。可能是重复问题:
限制浮点精度?
在C语言中,我需要从用户输入的数字中格式化出小数点后2位。
例如:
float x ;
printf("Enter number");
4.54234635
我需要在整个程序中打印并进行处理:
4.54
提前感谢。scanf("%.2f",&x);
我认为那将解决一个问题。
完整清单
Integer display
%d print as decimal integer
%6d print as decimal integer, at least 6 characters wide
%f print as floating point
%6f print as floating point, at least 6 characters wide
%.2f print as floating point, 2 characters after decimal point
如果进行所需的修改(scanf("%.2f",&x);
是最后一次输入),就能解决你的问题。
使用
scanf("%.2f",&number);
or
printf("%.2f", number);