C中的int * double 问题
//sNewValue = "482000" int数组
//iMonths = 87; int
//dRate = 0.009f; double
dNewValue = atof(sNewValue);
dPrice = dNewValue * (1.0 - iMonths * dRate);
printf("dPrice = %f * (1 - %d * %f) = %f\n",dNewValue,iMonths,dRate,dPrice );
计算完打印出来,竟然有问题。
dPrice = 482000.000000 * (1 - 87 * 0.009000)= 104594.016....
结果本应整数,但多了0.016.....
这样我再保留两位小数时就有问题了。这是为什么呢?
我的解决方法
dNewValue = atof(sNewValue);
fTemp1 = iMonths * dRate;
EXEC SQL select round(:fTemp1,3) into :fTemp1 from dual;
dPrice = dNewValue * (1.0 - fTemp1);
printf("dPrice = %f * (1 - %d * %f) = %f\n",dNewValue,iMonths,dRate,dPrice );
计算完打印出来,没有问题。
dPrice = 482000.000000 * (1 - 87 * 0.009000)= 104594.000000
这样对于本数据倒是对了,不知对于其他数据,会不会有什么问题呢?
或者还有什么更好的方法呢?