RoundTo() differences in roundings

Asked by At

IDE = Borland C++ Builder 6

I have a problem with the RoundTo function ()

otherwise it rounds even or odd numbers

if I have RoundTo (number, 0) where number = 46.5 then the result is 46 and if I have RoundTo (number, 0) where number = 45.5 then the result is also 46 search for a function that every time the decimal is 0.5 the result rounded up.

2 Answers

0
forritarik On

the ceil function is not good because it rounds numbers to integers and I need to round numbers also after a comma.

My number 46.5 is exactly 46.5 because I tested it by giving it directly to the function and the result is the same.

0
forritarik On

Maybe it's not an elegant solution but it works

double number, aftercomma;
int intnumber;

number = 46.5;
intnumber = (int)number;
aftercomma = number - intnumber;
if(aftercomma >= 0.5)
   {
    number = Ceil(number);
   }
else
   {
    number = Floor(number);
   }