I understand you want to round to the nearest
0.5
without using
System.Math
.
double[] values = new[] { 15.234, 15.617, 15.256, 15.25, 15.880, 15.7777 };
foreach (double v in values)
Console.WriteLine("{0} rounds to {1}", v, Round(v));
double Round(double value)
{
return ((int)((value+0.251)*2))/2.0;
}
Outputs:
15.234 rounds to 15
15.617 rounds to 15.5
15.256 rounds to 15.5
15.25 rounds to 15.5
15.88 rounds to 16
15.7777 rounds to 16
I believe in VB.NET, it would be
something like:
Dim values() As Double = { 15.234, 15.617, 15.256, 15.25, 15.880, 15.7777 };
For Each v As Double In values
Console.WriteLine(Round(v))
Next
Function Round(ByVal value As Double) As Double
Round = CInt(((value+0.251)*2))/2.0f
End Function