// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public class Program { public static decimal FlexRound(decimal x, decimal unit) { int n = (int)(1 / unit); if(unit * n != 1.00m) { throw new ArgumentException("Invalid unit: " + unit); } return ((decimal)((int)(x * n + 0.50m))) / n; //return Math.Ceiling(x * n) / n; } public static void Main(string[] args) { for(int i = 0; i < 100; i++) { decimal x = i / 100.00m; Console.WriteLine(x + " -> " + FlexRound(x, 0.05m)); } } } }