about "round" function in visual C++
-
Dear all I have one double number which i want to round to nearest decimal number in Visual C++. What is the function to do this REgards Monhi
double d =whatever;
int i = (int)(d + .5);:) Please note that, as pointed out by Rage it doesn't work properly with negative numbers See the fix here [^]. -- modified at 8:10 Tuesday 10th July, 2007
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
double d =whatever;
int i = (int)(d + .5);:) Please note that, as pointed out by Rage it doesn't work properly with negative numbers See the fix here [^]. -- modified at 8:10 Tuesday 10th July, 2007
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
d=-5.6 => i=-5, not -6. Is this ok ?
http://www.readytogiveup.com/[^] - Do something special today.
he probably should take the absolute value, and then apply it the original sign...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
d=-5.6 => i=-5, not -6. Is this ok ?
http://www.readytogiveup.com/[^] - Do something special today.
Rage wrote:
d=-5.6 => i=-5, not -6. Is this ok ?
No, of course. I (naively) assumed
d > 0
. But there is an easy fix to account for negative numbers:i = (int) ( d < .0 ? d -.5 : d + .5 );
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.