guy87

Hello,

I am working on a program, which needs to output numbers rounded in a certain way. In this program, a number needs to be read in and divided by a constant of the data type double. The constant is 3.78.

So, for example if the input number is 9000, 9000 / 3.78 = 2380.952. I need this number to be output as an integer. If I set the output variable as an integer, the program round and would output 2380. But I need the number rounded up, which would be 2381. Another example is, 100 / 3.78 = 26.455, which should be output as 27. Basically any number that has a decimal part, I need it rounded up. I caní»t find any way to do that. The only way I can think of is by using if statements. Something like:

If (num has decimal part)
              then convert to int and add 1
else
             convert to int

However, I can't use an if statement (no loops either), because this is a school excercise which has some restrictions. I have finished the exercise, except for this part. This is the only part I can't figure out

Can somebody please help

                                                                                                                Thanks in advance.


Re: Visual C++ Language number rounding

Alex Farber

2380.952 should be 2381.
26.455 should be 26 (or 27 as you write )

If you want 27 in the last case, just add 1 to the result. To ruound positive float number properly, add 0.5 and then convert to integer:

float f = ... ;
int n = (int)(f + 0.5f);





Re: Visual C++ Language number rounding

Marcelo Lopez Ruiz - MSFT

I am uncertain as to what behavior you are looking for - let me briefly explain some of the things that might be useful for these kind of problems.

If you want to round to the closest integer that is less than or equal (essentially truncating all digits beyond the decimal separator), simply assigning the value to an int variable will do the trick - but this is not what you want.

If you want to round to the closest integer number, you can add 0.5 to the result and then assign to an integer to truncate the remainder. Note that you have to be careful about what behavior you want to for exact "in the middle" values like 1.5.

If you want to round to the closest integer number towards positive infinity, you can truncate, and if the truncation cast to a double is different from the result, add one to the result - something along these lines.

const double a = 3.78;

double result = number / a;

int result_truncated = result;

if (result == (double)result_truncated)

return result_truncated;

else

return result_truncated + 1;

In practice, you may want to introduce a small margin of error to account for precision errors during operations with floating-point numbers, and compare a range instead of comparing for exact equality.





Re: Visual C++ Language number rounding

Simple Samples

Rounding has been done before computers existed. If the mathematics is understood then it is very easy to understand the programming required. It is so simple that there is nothing specific in the C/C++ langauges. Therefore technically this is not a C++ question. Hopefully there are articles available though the internet that explain the mathematics.




Re: Visual C++ Language number rounding

Sarath.

You can make use of
floor (e.g 2.8 -> 2 ) or ceil (e.g 2.8 -> 3) C-Runtime functions for rounding down/up purpose.

Please refer the link for description and sample.

HTH