[英]How to convert int to Double in C programming
int num1, num2;
double average;
average=(double)(num1+num2)/2;
printf("average: %d", average);
My test printf shows average as: 0我的测试 printf 显示平均值为:0
This is maybe too easy, but I can't see it.这可能太简单了,但我看不到。 My inputs are both "int" and the average is "double" but somehow it is not calculating right?我的输入都是“int”,平均值是“double”,但不知何故计算不正确?
You're using the wrong format specifier to printf
. 您对printf
使用了错误的格式说明符。
The %d
format specifier expects an int
argument, but you're passing a double
. %d
格式说明符期望使用int
参数,但是您要传递double
。 Using the wrong format specifier invokes undefined behavior . 使用错误的格式说明符会导致未定义的行为 。
To print a double
, use %f
. 要打印double
,请使用%f
。
printf("average: %f\n", average);
No need to modify the statement average=(double)(num1+num2)/2;
无需修改语句average=(double)(num1+num2)/2;
to get expected result inside printf
use %f
instead of %d
要在printf
内部获得预期的结果,请使用%f
而不是%d
(num1+num2)
is performed, result of this is of integral
type. 执行第一个(num1+num2)
,其结果为integral
类型。 lets say 15
. 可以说15
。 Next when you do (double)15/2
result is of floating
type which is 7.500000
. 接下来,当您执行(double)15/2
结果是7.500000
的floating
型。 average = (double)7.500000
average holds 7.500000
but since you printed in %d
you are getting 0
as its undefined behavior. 来自上一步的average = (double)7.500000
平均值保持7.500000
但是由于您在%d
打印,因此未定义行为得到0
。 instead use %f
而是使用%f
Here is the working one 这是工作中的一个
int main() {
int num1 = 7, num2 = 8;
double average;
average = (double)(num1 + num2)/2;
printf("average: %f\n", average);
return 0;
}
int num1, num2;
double average;
average=(num1+num2)/2.; // Using a decimal point forces 2 to be a double.
printf("average: %f\n", average); // Use the correct specifier for double: %f
You should use printf("average= %f",avearge); 您应该使用printf(“ average =%f”,avearge); instead of using "%d" to print the average value.I think it will solve your issues... 而不是使用“%d”来打印平均值。我认为它将解决您的问题...
Integer division yields an integer result: 1/2 == 0
, 5/3 == 1
, etc. If you want a floating point result, at least one of the operands must be a floating-point type: 1/2.0f == 0.5f
, 5/3.0 == 1.6667
, etc. 整数除法可得出整数结果: 1/2 == 0
5/3 == 1
, 1/2.0f == 0.5f
。如果要浮点结果,则至少一个操作数必须是浮点类型: 1/2.0f == 0.5f
, 5/3.0 == 1.6667
等
So, you'll want to divide your sum by 2.0
, not 2
: 因此,您需要将总和除以2.0
而不是2
:
average = (num1 + num2)/2.0;
Secondly, you need to use the %f
format specifier for floating-point output: 其次,您需要将%f
格式说明符用于浮点输出:
printf( "average: %f\n", average );
If you want a floating point result, at least one of the operands must be a floating-point type如果你想要一个浮点数结果,至少有一个操作数必须是浮点数类型
This is my solution:这是我的解决方案:
average = (num1 + num2)/(double)2;平均值 = (num1 + num2)/(double)2;
printf ("Value of 1/2 is: %.4f\n", (double)(1/2)); printf ("1/2 的值为: %.4f\n", (double)(1/2));
Value of 1/2 is: 0.0000 1/2 的值为:0.0000
printf ("Value of 1/2 is: %.4f\n", (1/(double)2)); printf ("1/2 的值为: %.4f\n", (1/(double)2));
Value of 1/2 is: 0.5000 1/2 的值为:0.5000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.