编写一个函数float fun(double h),函数的功能是对变量h中的值保留2位小数,并对第3位进行四舍五入(规定h中的值为正数)。如:h值为8.3243
编写一个函数float fun(double h),函数的功能是对变量h中的值保留2位小数,并对第3位进行四舍五入(规定h中的值为正数)。如:h值为8.32433,则函数返回8.32.
2012-03-11 16:06
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
float DoubleToFloat(double h);
void main(void)
{
printf_s("%.2f\n", DoubleToFloat(123456.3263));
_getch();
}
float DoubleToFloat(double h)
{
float ret = 0.0;
char buffer[20];
sprintf_s(buffer, "%.2f", h);
if (strlen(buffer) <= 9)
{
sscanf(buffer, "%f", &ret);
}
return ret;
}
2012-03-11 20:13

2012-03-11 20:28

2012-03-11 23:32

2012-03-12 12:09
程序代码:#include <stdio.h>
float fun(double h);
int main(void)
{
double n;
scanf("%lf",&n);
printf("%.2lf\n",fun(n));
return 0;
}
float fun(double h)
{
double m;
m = h - (int)h;
if (m / 100.0 >= 0.005)
return (int)h + m / 100.0 + 0.01;
return h;
}

2012-03-12 13:14
2012-03-12 14:23
2012-03-12 15:17
float x = 140.20F;
printf_s("%f\n", x);

2012-03-12 15:37
2012-03-13 22:50