In the .NET Framework, the System.Math class has methods for Ceiling, Floor, Round, and Truncate. Round has an overload for specifying the number of decimal places to round to but it doesn't support other intervals, for example: rounding to the nearest five cents.
The mathematics involved for supporting other intervals is quite simple when you know it but getting there and verifying it can take a few moments. Here is the general solution to save time next time I need it, or if someone else is looking for the same thing:
decimal offset = 0.05M; // interval we are rounding to (5 cents)decimal value = 1.23M; // value to be roundeddecimal temp = value / offset;temp = MyRoundToInteger(temp); // toward zero or banker's rounding functiondecimal result = temp * offset;
You can exchange MyRoundToInteger for any of the afore mentioned Math class methods or even one of your own custom rounding techniques, as long as it rounds to zero decimal places
dasBlog theme based on original by Mads Kristensen
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.