编写一个函数float fun(double h),函数的功能是对变量h中的值保留2位小数,并对第3位进行四舍五入(规定h中的值为正数)。如:h值为8.3243
编写一个函数float fun(double h),函数的功能是对变量h中的值保留2位小数,并对第3位进行四舍五入(规定h中的值为正数)。如:h值为8.32433,则函数返回8.32.#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; }
#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; }
float x = 140.20F; printf_s("%f\n", x);