关于C程序显示十进制值的问题 [英] Problem regarding displaying decimal values in C Program

查看:111
本文介绍了关于C程序显示十进制值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据类型浮动显示十进制数。默认情况下,我的编译器最多可显示到6小数。我希望看到只有两位小数。对于例如,当编译器执行操作C = 2/3
它会显示0.666666667。我希望看到只在我的输出画面0.67。
所以我应该做哪些必要的修改在C程序?

the data-type 'float' displays decimal numbers. by default my compiler displays up-to 6 decimals. i want to see only two decimals. for eg , when the compiler performs the operation "c=2/3" it displays "0.666666667". i want to see only "0.67" in my output screen. so what necessary changes should i make in the C program?

推荐答案

您可以输出使用的printf 的数量时使用格式说明将其限制为2位小数。

You can use a format specifier to limit it to 2 decimal places when outputting the number using printf.

int main() {
  double d = 2.0 / 3.0;
  printf("%.2f\n",d);
  return 0;
}

下面是输出:

---------- Capture Output ----------
> "c:\windows\system32\cmd.exe" /c c:\temp\temp.exe
0.67

> Terminated with exit code 0.

这篇关于关于C程序显示十进制值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆