我有这个静态方法,它接收一个双精度并“剪切”它的小数尾部,在点之后留下两位数。几乎一直在工作。我注意到当它收到 2.3 时,它会变成 2.29。0.3、1.3、3.3、4.3 和 102.3 不会发生这种情况。代码基本上将数字乘以 100 使用 modf 将整数值除以 100 并返回它。这里的代码捕获了这个特定的数字并打印出来:
static double dRound(double number) {
bool c = false;
if (number == 2.3)
c = true;
int factor = pow(10, 2);
number *= factor;
if (c) {
cout << " number *= factor : " << number << endl;
//number = 230;// When this is not marked as comment the code works well.
}
double returnVal;
if (c){
cout << " fractional : " << modf(number, &returnVal) << endl;
cout << " integer : " <<returnVal << endl;
}
modf(number, &returnVal);
return returnVal / factor;
}
它打印出来:
数字 *= 系数:230
小数:1
整数:229
有谁知道为什么会这样,我该如何解决这个问题?谢谢你,祝你周末愉快。