Suppose I have two integers A and B, where B is not 0. I want to round A to the nearest integral multiple of B. How can I do it?
Here is a way to do it (in pseudocode):
C = the quotient of A divided by B
D = the remainder of A divided by B
Result = C times B
D = absolute value of D
E = absolute value of B
D = 2 times D
If D is greater than or equal to E
If A is greater than 0
Result = Result + E
Else
Result = Result - E
End-If
End-If
The final value we want is the value of Result.
All the variables involved here are integers.
To understand this, you may need to work through a few examples using various values (even odd, positive, negative).