c# round float to closest int code example
Example 1: c# round to closest multiple
int value = 30;
int factor = 16;
int nearestMultiple =
(int)Math.Round(
(value / (double)factor),
MidpointRounding.AwayFromZero
) * factor;
Example 2: round to the nearest whole number c#
var roundedA = Math.Round(1.1, 0);
var roundedB = Math.Round(1.5, 0, MidpointRounding.AwayFromZero);
var roundedC = Math.Round(1.9, 0);
var roundedD = Math.Round(2.5, 0);
var roundedE = Math.Round(2.5, 0, MidpointRounding.AwayFromZero);
var roundedF = Math.Round(3.49, 0, MidpointRounding.AwayFromZero);