Python编程 输入一个小数,对第三位小数进行四舍五入 保留两位小数 不能...
发布网友
发布时间:2022-04-26 08:59
我来回答
共2个回答
热心网友
时间:2022-04-07 13:03
#ifndef HAVE_COPYSIGN
double
copysign(double x, double y)
{
/* use atan2 to distinguish -0. from 0. */
if (y > 0. || (y == 0. && atan2(y, -1.) > 0.)) {
return fabs(x);
} else {
return -fabs(x);
}
}
#endif /* HAVE_COPYSIGN */
#ifndef HAVE_ROUND
double
round(double x)
{
double absx, y;
absx = fabs(x);
y = floor(absx);
if (absx - y >= 0.5)
y += 1.0;
return copysign(y, x);
}
#endif /* HAVE_ROUND */
热心网友
时间:2022-04-07 14:21
float M;//输入的小数
unsigned int x1 = (unsigned int)(M - (int)M) * 10;
unsigned int x2 = (unsigned int)(M - (int)M) * 100%10;
unsigned int x3 = (unsigned int)(M - (int)M) * 1000%10;
if(x3 >= 5)
{
x2 += 1;
}
M = (int)M + ((float)x1)/10 + ((float)x2)/100;