C程序自动舍入

C Program automatically rounds

编辑:问题已经解决,不过我现在会尽量说清楚。

当我编写一个计算圆面积的程序时,我将“3.1415926535”分配给了一个常量浮点数 Pi。当我在我的程序中将它用作 areaCircle = Pi * radius * radius 时,其中 radius 是一个整数而 areaCircle 是一个浮点数,程序打印出类似“7.000000”的内容。我原以为它是 return 小数,例如“7.123456”或其他东西

编辑:这是代码,如果你们需要的话:

int radius
float circumference,areaCircle;
const int PI = 3.14159265358979323846264338327;
printf("Enter the radius of the circle: \n");
scanf("%d",&radius);
circumference = PI*2*radius;
areaCircle = PI*radius*radius;
printf("The circumference of a circle with radius %d is %f and its area is %f\n",radius,circumference,areaCircle);

在您发布的代码中,您已将 PI 声明为 int。尝试:

const float PI = 3.14159265358979323846264338327;

It'll be very nice if you make your question more clearly.

虽然我有一些建议...

  • 你的程序显示 areaCircle Value as floating data ,因为,areaCircle是浮动的。它也在计算一个浮点数。

  • Logically -半径 can't be Integer 类型。

  • 可以设置all data types as float / double.

  • 也可以用这个into语句来 控制小数点后的数字 %.2f%.0f 或您需要的任何值。