This accepts values from a user that needs to show up in a intervals of some number. In this case, we will say intervals of 5.
Is there a better way to do this?
public static int RoundTo(this int value, int roundTo)
{
var midPoint = roundTo / 2.0;
var remainder = value % roundTo;
var result = remainder < midPoint
? (value - remainder) //round down
: (value + (roundTo - remainder)); //round up
return result;
}
Example
Console.WriteLine(7.RoundTo(5)); //5
Console.WriteLine(8.RoundTo(5)); //10