我有以下源代码,但结果不是舍入到小数点后两位。四舍五入浮点数的问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(int argc, char *argv[])
{
float x1=0;
float x2 = 0;
float result=0;
x1 = 8961.650391;
result = x1 * 100 + 0.5;
result = (float)floor(result);
printf("Result = <%f>\n", result);
result = result/100;
x2 = result;
printf("x2 = <%f>\n", x2);
return 0;
}
请帮忙解决问题。
Result = <896165.000000>
x2 = <8961.650391>
如何获取x3 = 8961.650000?
但我需要四舍五入的价值,所以我需要X2 = <8961.65> – user5240895
添加了roundf功能,这将帮助你圆也。 ,查看更新后的答案 –