As a follow up to my previous Shakespearean-titled post on rounding, I decided to provide a quick follow up to provide a solution for midpoint rounding in Silverlight. Based on some traffic that I’ve been getting it looks like people are searching for a rounding solution in Silverlight since the Silverlight core framework lacks the traditional Math.Round() overloads we get with the full .NET framework that allow specifying the desired midpoint rounding behavior.

*Remember, Silverlight contains a pared-down version of the .NET framework.*

So, here is a simple solution to take what we learned in the previous post and develop our own MathExt helper class that provides static Round methods similar to the Math.Round() methods that allow us to specify the desired midpoint rounding behavior. Using this you can now control midpoint rounding behavior in Silverlight.

public enum MidpointRounding { ToEven, AwayFromZero } public static class MathExt { public static decimal Round(decimal d, MidpointRounding mode) { return MathExt.Round(d, 0, mode); } /// <summary> /// Rounds using arithmetic (5 rounds up) symmetrical (up is away from zero) rounding /// </summary> /// <param name="d">A Decimal number to be rounded.</param> /// <param name="decimals">The number of significant fractional digits (precision) in the return value.</param> /// <returns>The number nearest d with precision equal to decimals. If d is halfway between two numbers, then the nearest whole number away from zero is returned.</returns> public static decimal Round(decimal d, int decimals, MidpointRounding mode) { if (mode == MidpointRounding.ToEven) { return decimal.Round(d, decimals); } else { decimal factor = Convert.ToDecimal(Math.Pow(10, decimals)); int sign = Math.Sign(d); return Decimal.Truncate(d * factor + 0.5m * sign) / factor; } } }

Or if you prefer an extension-method approach:

public enum MidpointRounding { ToEven, AwayFromZero } public static class DecimalExtensions { public static decimal Round(this decimal d, MidpointRounding mode) { return d.Round(0, mode); } /// <summary> /// Rounds using arithmetic (5 rounds up) symmetrical (up is away from zero) rounding /// </summary> /// <param name="d">A Decimal number to be rounded.</param> /// <param name="decimals">The number of significant fractional digits (precision) in the return value.</param> /// <returns>The number nearest d with precision equal to decimals. If d is halfway between two numbers, then the nearest whole number away from zero is returned.</returns> public static decimal Round(this decimal d, int decimals, MidpointRounding mode) { if (mode == MidpointRounding.ToEven) { return decimal.Round(d, decimals); } else { decimal factor = Convert.ToDecimal(Math.Pow(10, decimals)); int sign = Math.Sign(d); return Decimal.Truncate(d * factor + 0.5m * sign) / factor; } } }

Remember, the non-extension-method version could easily be used in any legacy .NET projects that still may be on 1.0 or 1.1 to give us the ability to control midpoint rounding behavior which didn’t arrive in the framework until .NET 2.0.

Once again, Happy Rounding!