Here's a sample LINQPad program that demonstrates one way to do it. I tested this by running all those numbers through Excel to verify that it behaves the same way:
void Main()
{
Verify(ROUND(3206.618652, 2), 3206.62).Dump();
Verify(ROUND(3206.618652, 1), 3206.6).Dump();
Verify(ROUND(3206.618652, 0), 3207).Dump();
Verify(ROUND(3206.618652, -1), 3210).Dump();
Verify(ROUND(3206.618652, -2), 3200).Dump();
Verify(ROUND(3207.618652, 2), 3207.62).Dump();
Verify(ROUND(3207.618652, 1), 3207.6).Dump();
Verify(ROUND(3207.618652, 0), 3208).Dump();
Verify(ROUND(3207.618652, -1), 3210).Dump();
Verify(ROUND(3207.618652, -2), 3200).Dump();
Verify(ROUND(3205.618652, 2), 3205.62).Dump();
Verify(ROUND(3205.618652, 1), 3205.6).Dump();
Verify(ROUND(3205.618652, 0), 3206).Dump();
Verify(ROUND(3205.618652, -1), 3210).Dump();
Verify(ROUND(3205.618652, -2), 3200).Dump();
Verify(ROUND(-3206.618652, 2), -3206.62).Dump();
Verify(ROUND(-3206.618652, 1), -3206.6).Dump();
Verify(ROUND(-3206.618652, 0), -3207).Dump();
Verify(ROUND(-3206.618652, -1), -3210).Dump();
Verify(ROUND(-3206.618652, -2), -3200).Dump();
Verify(ROUND(-3207.618652, 2), -3206.62).Dump();
Verify(ROUND(-3207.618652, 1), -3206.6).Dump();
Verify(ROUND(-3207.618652, 0), -3207).Dump();
Verify(ROUND(-3207.618652, -1), -3210).Dump();
Verify(ROUND(-3207.618652, -2), -3200).Dump();
Verify(ROUND(-3205.618652, 2), -3205.62).Dump();
Verify(ROUND(-3205.618652, 1), -3205.6).Dump();
Verify(ROUND(-3205.618652, 0), -3206).Dump();
Verify(ROUND(-3205.618652, -1), -3210).Dump();
Verify(ROUND(-3205.618652, -2), -3200).Dump();
Verify(ROUND(3205.4, 0), 3204).Dump();
Verify(ROUND(3205.6, 0), 3205).Dump();
Verify(ROUND(-4.4, 0), -4).Dump();
Verify(ROUND(-4.5, 0), -5).Dump();
Verify(ROUND(-4.6, 0), -5).Dump();
Verify(ROUND(4.4, 0), 4).Dump();
Verify(ROUND(4.5, 0), 5).Dump();
Verify(ROUND(4.6, 0), 5).Dump();
}
public static string Verify(double value, double expected)
{
if (Math.Abs(value - expected) < 1e-8)
return string.Empty;
return value + " is not equal (enough) to " + expected;
}
public static double ROUND(double value, int decimals)
{
if (decimals < 0)
{
var factor = Math.Pow(10, -decimals);
return ROUND(value / factor, 0) * factor;
}
return Math.Round(value, decimals, MidpointRounding.AwayFromZero);
}