| | |
Round Up Round down(truncate)
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2005
Posts: 1,761
Reputation:
Solved Threads: 283
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,761
Reputation:
Solved Threads: 283
>>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
Views: 2884 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







...