首页 > 编程知识 正文

c语言保留两位小数输出格式,printf格式化输出

时间:2023-05-05 09:10:02 阅读:110929 作者:1112

printf ) )格式打印:

打印输出的变量类型为double,scanf (和printf )使用占位符%lf。

# include stdio.h # include math.hint main ({ double base; 双高度; double hypotenuse; scanf('%lf%lf )、base和height ); hypotenuse=sqrt(pow(base,2 ) pow ) height,2 ); printf('%lf(n ),hypotenuse base height; printf('%lf ',base * height/2 ); 返回0; }输出的数据带有小数点后6位。

printf )的输出结果保持两位小数,printf ) )的第一个参数的占位符(lf )变更为“%.2f”时,意味着取第二个参数(double型)的值输出。

# include stdio.h # include math.hint main ({ double base; 双高度; double hypotenuse; scanf('%lf%lf )、base和height ); hypotenuse=sqrt(pow(base,2 ) pow ) height,2 ); printf('%.2f(n ),hypotenuse base height ); printf('%.2f )、base * height/2 ); 返回0; }效果:

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。