Rounding double to a provided decimal place
-
Hi All. A very simple question, but so far not able to find any concrete solution. I just want to round a decimal value to a provided decimal place. I have found few functions to do the same, but they also fail for some double values...like...
double Round1(double dbVal, int nPlaces)
{
const double dbShift = pow(10.0, nPlaces);
return floor(dbVal * dbShift + 0.5) / dbShift;
}double Round2(double value,int pos)
{
double returnValue;
double tens = exp(pos*log(10.0));
if((value - floor(value*tens)/tens)*tens >= 0.5)
returnValue = ceil(value*tens)/tens
else
returnValue = floor(value*tens)/tens;return returnValue;
}
Both of above function fails in case the double value is 10430.889999999999 464.45999999999998 3294.5100000000002 Please suggest. Thanks PanB
-
Hi All. A very simple question, but so far not able to find any concrete solution. I just want to round a decimal value to a provided decimal place. I have found few functions to do the same, but they also fail for some double values...like...
double Round1(double dbVal, int nPlaces)
{
const double dbShift = pow(10.0, nPlaces);
return floor(dbVal * dbShift + 0.5) / dbShift;
}double Round2(double value,int pos)
{
double returnValue;
double tens = exp(pos*log(10.0));
if((value - floor(value*tens)/tens)*tens >= 0.5)
returnValue = ceil(value*tens)/tens
else
returnValue = floor(value*tens)/tens;return returnValue;
}
Both of above function fails in case the double value is 10430.889999999999 464.45999999999998 3294.5100000000002 Please suggest. Thanks PanB
-
Hi All. A very simple question, but so far not able to find any concrete solution. I just want to round a decimal value to a provided decimal place. I have found few functions to do the same, but they also fail for some double values...like...
double Round1(double dbVal, int nPlaces)
{
const double dbShift = pow(10.0, nPlaces);
return floor(dbVal * dbShift + 0.5) / dbShift;
}double Round2(double value,int pos)
{
double returnValue;
double tens = exp(pos*log(10.0));
if((value - floor(value*tens)/tens)*tens >= 0.5)
returnValue = ceil(value*tens)/tens
else
returnValue = floor(value*tens)/tens;return returnValue;
}
Both of above function fails in case the double value is 10430.889999999999 464.45999999999998 3294.5100000000002 Please suggest. Thanks PanB
Is it a possibility for you to convert this to CString (if using MFC) or char* (using sprintf_s)? If affirmative, use the format specifiers and call API CString::Format or sprintf_s and then convert back to double value when returning from your function.
I am a HUMAN. I have that keyword in my name........ ;-)_AnsHUMAN_
-
Is it a possibility for you to convert this to CString (if using MFC) or char* (using sprintf_s)? If affirmative, use the format specifiers and call API CString::Format or sprintf_s and then convert back to double value when returning from your function.
I am a HUMAN. I have that keyword in my name........ ;-)_AnsHUMAN_
Converting double to CString can be done, but to round off the same will take lots of permutations and combinations because we dont know upto what decimal place we need rounding. If I am wrong then please let me know, and in case you have some pointers then do let me know that as well. Thanks PanB
-
Hi All. A very simple question, but so far not able to find any concrete solution. I just want to round a decimal value to a provided decimal place. I have found few functions to do the same, but they also fail for some double values...like...
double Round1(double dbVal, int nPlaces)
{
const double dbShift = pow(10.0, nPlaces);
return floor(dbVal * dbShift + 0.5) / dbShift;
}double Round2(double value,int pos)
{
double returnValue;
double tens = exp(pos*log(10.0));
if((value - floor(value*tens)/tens)*tens >= 0.5)
returnValue = ceil(value*tens)/tens
else
returnValue = floor(value*tens)/tens;return returnValue;
}
Both of above function fails in case the double value is 10430.889999999999 464.45999999999998 3294.5100000000002 Please suggest. Thanks PanB
-
Hi All. A very simple question, but so far not able to find any concrete solution. I just want to round a decimal value to a provided decimal place. I have found few functions to do the same, but they also fail for some double values...like...
double Round1(double dbVal, int nPlaces)
{
const double dbShift = pow(10.0, nPlaces);
return floor(dbVal * dbShift + 0.5) / dbShift;
}double Round2(double value,int pos)
{
double returnValue;
double tens = exp(pos*log(10.0));
if((value - floor(value*tens)/tens)*tens >= 0.5)
returnValue = ceil(value*tens)/tens
else
returnValue = floor(value*tens)/tens;return returnValue;
}
Both of above function fails in case the double value is 10430.889999999999 464.45999999999998 3294.5100000000002 Please suggest. Thanks PanB
You won't be able to do something like that due to the nature of floating points. I suggest you google for floating point precision for more information about the subject (e.g. on wikipedia[^]). The good news is that you most probably don't have to care about it. Why do you need to be precise like that ? Most of the time a such a precision error is perfectly acceptable.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Is it a possibility for you to convert this to CString (if using MFC) or char* (using sprintf_s)? If affirmative, use the format specifiers and call API CString::Format or sprintf_s and then convert back to double value when returning from your function.
I am a HUMAN. I have that keyword in my name........ ;-)_AnsHUMAN_
That's still won't solve the problem, you would still be limited by floating point precision.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Hi All. A very simple question, but so far not able to find any concrete solution. I just want to round a decimal value to a provided decimal place. I have found few functions to do the same, but they also fail for some double values...like...
double Round1(double dbVal, int nPlaces)
{
const double dbShift = pow(10.0, nPlaces);
return floor(dbVal * dbShift + 0.5) / dbShift;
}double Round2(double value,int pos)
{
double returnValue;
double tens = exp(pos*log(10.0));
if((value - floor(value*tens)/tens)*tens >= 0.5)
returnValue = ceil(value*tens)/tens
else
returnValue = floor(value*tens)/tens;return returnValue;
}
Both of above function fails in case the double value is 10430.889999999999 464.45999999999998 3294.5100000000002 Please suggest. Thanks PanB
You cannot alter the precision of floating point numbers this way. Your rounding should occur when you wish to display the number as a string. A Google search for "floating point value" will find lots of papers that explain why this is so.
I must get a clever new signature for 2011.