Click to See Complete Forum and Search --> : Round() SIMPLE Question


lior6543
April 14th, 2005, 04:17 AM
Hello people! It has been a long time since my last visit...
I got a small question for you all:
I would like to round a double variable to 0.25.

Examples:
==========
21.02 -> 21.00
14.76 -> 14.75
48.99 -> 49.00 or alternatively to 48.75.

Got the point? ...And so no...

dumbquestion
April 14th, 2005, 09:38 AM
This works, I think. Not exactly elegant though...
the numericUpDown1 on the form contains the number to be rounded, e.g. 14.86.
the numericUpDown2 on the form contains the rounding value, e.g. 0.25.


private: System::Void button3_Click(System::Object * sender, System::EventArgs * e)
{
double num, intpart, decpart, round;
num = Convert::ToDouble(numericUpDown1->Value);
round = Convert::ToDouble(numericUpDown2->Value);
intpart = Math::Floor(num);
decpart = num - intpart;
decpart = round * Math::Round(decpart / round);
num = intpart + decpart;
MessageBox::Show(Convert::ToString(num));
}

lior6543
April 14th, 2005, 03:05 PM
Thanks!
I can live with this solution.
if anyone can give me a more elegant one, it would be great.