At first it is important to know that floating-point numbers are approximated. See the link provided by @Greg Hewgill to see why this problem is not completely solvable.
But here are a few solutions to the problem that are likely to satisfy your needs:
Probably the best method, but less effective:
char sz[64]; double lf = 0.600000002; sprintf(sz, "%.4lf\n", lf); //sz contains 0.6000 double lf2 = atof(sz); //lf == 0.600000002; //lf2 == 0.6000 printf("%.4lf", lf2); //print 0.6000
A more efficient way, but probably less accurate:
double lf = 0.600000002; int iSigned = lf > 0? 1: -1; unsigned int uiTemp = (lf*pow(10, 4)) * iSigned;
Brian R. Bondy Nov 20 '08 at 0:49 2008-11-20 00:49
source share