为什么数字在C中被舍入? [英] How come number is being rounded in C?

查看:77
本文介绍了为什么数字在C中被舍入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输出总是向上或向下舍入?而不是显示十进制浮点数?任何帮助都非常感谢!

谢谢!

这是代码:



The output always rounds up or down? instead of showing a decimal float? Any help is well appreciated!
Thanks!
Here is the code:

#include<stdio.h>
#include<conio.h>

 void DisplayInstructions();
 int CalculateAverage(int a,int b, int c);
 float DisplayAnswer(float ans);

 void main()
 {
     int Integer1 = 0, Integer2 = 0, Integer3 = 0;
     float answer;
     DisplayInstructions();
     scanf_s("%d", &Integer1);
     scanf_s("%d", &Integer2);
     scanf_s("%d", &Integer3);
     answer = CalculateAverage( Integer1, Integer2, Integer3);
     DisplayAnswer(answer);
     _getch();
 }

 void DisplayInstructions()
 {
     printf("Enter 3 Integers seperated by the ENTER key\n");
 }

 int CalculateAverage(int a, int b, int c)
 {
     float average, num = 3;
     average = ( a + b + c )/num;

     return average ;
 }

 float DisplayAnswer(float ans)
 {
     printf("Answer=%f\n", ans);
 }

推荐答案

如果您返回 int ,您会有什么期望? ?将 int CalculateAverage 更改为 int CalculateAverage



-SA
What would you expect if you return int? Change "int CalculateAverage" to float "int CalculateAverage".

—SA


更改int CalculateAnswer以返回浮动。在printf中更改格式字符串为2个小数点。查看cplusplus.com了解详情。
Change int CalculateAnswer to return a float. In printf change format string for 2 decimal points. Look up cplusplus.com for details.


这篇关于为什么数字在C中被舍入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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