| | |
Round Up Round down(truncate)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 264
WARNING:Code based on my logic. Not compiled and tested in reality.
//round to closest integer.
double d = 2.49;
int i = (int)(d + 0.05);
cout << i << endl;
d = 2.51;
i = (int)(d + 0.05);
cout << i << endl;
//round to closest tenth (on first decimal point)
d = 2.41;
double temp = d * 10;
i = (int)(temp + 0.5);
temp = (double)(i/10);
cout << temp << endl;
d = 2.47;
temp = d * 10;
i = (int)(temp + 0.5);
temp = (double)(i/10);
cout << temp << endl;
//round to closest integer.
double d = 2.49;
int i = (int)(d + 0.05);
cout << i << endl;
d = 2.51;
i = (int)(d + 0.05);
cout << i << endl;
//round to closest tenth (on first decimal point)
d = 2.41;
double temp = d * 10;
i = (int)(temp + 0.5);
temp = (double)(i/10);
cout << temp << endl;
d = 2.47;
temp = d * 10;
i = (int)(temp + 0.5);
temp = (double)(i/10);
cout << temp << endl;
Klatu Barada Nikto
•
•
Join Date: Aug 2007
Posts: 10
Reputation:
Solved Threads: 2
double RoundDouble(double doValue, int nPrecision)
{
static const double doBase = 10.0;
double doComplete5, doComplete5i;
doComplete5 = doValue * pow(doBase, (double) (nPrecision + 1));
if(doValue < 0.0)
doComplete5 -= 5.0;
else
doComplete5 += 5.0;
doComplete5 /= doBase;
modf(doComplete5, &doComplete5i);
return doComplete5i / pow(doBase, (double) nPrecision);
}
{
static const double doBase = 10.0;
double doComplete5, doComplete5i;
doComplete5 = doValue * pow(doBase, (double) (nPrecision + 1));
if(doValue < 0.0)
doComplete5 -= 5.0;
else
doComplete5 += 5.0;
doComplete5 /= doBase;
modf(doComplete5, &doComplete5i);
return doComplete5i / pow(doBase, (double) nPrecision);
}
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 264
>>Lerner, check up your code again. It's wrong
Yup; looks like this,
i = (int)(d + 0.05);
should be this:
i = (int)(d + 0.5);
Any other (il)logic errors (and there may be some) will need to wait for me to check it with a compiler. The theory remains intact even if I stub my toe, though. Thanks for pointing out my error!
There are certainly other ways to round and I suspect that using any of the other approaches already suggested and those yet to be suggested will also work.
Yup; looks like this,
i = (int)(d + 0.05);
should be this:
i = (int)(d + 0.5);
Any other (il)logic errors (and there may be some) will need to wait for me to check it with a compiler. The theory remains intact even if I stub my toe, though. Thanks for pointing out my error!
There are certainly other ways to round and I suspect that using any of the other approaches already suggested and those yet to be suggested will also work.
Klatu Barada Nikto
Use modf() function to splt integer and fractional parts of the double value before other operations (change a sign of negative value before other stuff then restore right sign after rounding). You lost some precision and may catch overflow with direct multiplication. Have a look to addition of 0.5 in Lerner's post (no need in if-else).
Using (twice) of relatively slow pow function is not the best idea. Consider another approach(es)...
Using (twice) of relatively slow pow function is not the best idea. Consider another approach(es)...
![]() |
Similar Threads
- Computing Resistor values using Switch (C++)
- Code for getting tile info (Game Development)
- simple cash register (pos) problem - please help!! (VB.NET)
- This ought to be simple - extra spaces (PHP)
Other Threads in the C++ Forum
- Previous Thread: Linked List: Search and Modify Help
- Next Thread: having trouble with if statement and string
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets







...