int doubleToInt(double dVal, int nMagnification)
{
double dCorrect = 1.0 / (nMagnification * 100);
int n1 = (int)(dVal * nMagnification + dCorrect);
return n1;
}
测试代码:
double d1 = 1.0;
int k0 = 100;
double dStep = 1.0 / k0;
double dCorrect = 1.0 / (k0 * 100);
for (int i = 0; i < k0; i++) {
int n1 =(int) (d1 * k0 + dCorrect);
std::string str;
str=std::to_string(d1 * k0 + dCorrect);
int n2 = atoi(str.c_str());
printf("%.03f\t%d\t\t",d1,n1);
if (i % 4 == 3)
printf("\r\n");
d1 += dStep;
}
return 1;