Adam Anderly

Husband, Father, Developer

  • About
  • Blog
  • Archives
  • Plugins
    • WooCommerce MailChimp
    • Wistia Responsive
  • Contact

Copyright © 2025
Adam Anderly ยท Log in

Silverlight Midpoint Rounding Solution

August 8, 2009 By Adam 8 Comments

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!

public enum MidpointRounding
{
AwayFromZero,
ToEven
}
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;
}
}
}

Share this:

  • Twitter
  • LinkedIn
  • Email
  • Print
  • More
  • Reddit

Filed Under: .NET, Silverlight Tagged With: .NET, Rounding, Silverlight

RSS

RSS Feed

Subscribe

Enter your email address to subscribe and receive new posts by email.

Find It Here

Top Posts

  • Archives
  • Cross-Cutting Concerns with MediatR Pipeline Behaviors

Recent Posts

  • Cross-Cutting Concerns with MediatR Pipeline Behaviors
  • Laravel User Timezone Aware Trait
  • Useful Laravel Model Traits: Gravatar
  • Laravel Transformable – An Eloquent Model Trait for Consumable Models
  • Gravity Forms + Microsoft Dynamics CRM

Categories

  • .NET (12)
  • ASP.NET Core (1)
  • ASP.NET MVC (7)
  • Dependency Injection (3)
  • Laravel (3)
  • Laravel Traits (2)
  • MediatR (1)
  • Node.js (2)
  • NuGet (5)
  • PHP (2)
  • REST (4)
  • Silverlight (5)
  • Uncategorized (3)
  • WordPress (7)
  • WordPress Plugins (7)

Tags

.NET APIs ASP.NET Core ASP.NET MVC Azure Caching cli CRM Dependency Injection dns dnsimple Dynamics Dynamics CRM Expansive Fallback Gravity Forms iPhone Laravel Laravel Traits MailChimp MediatR Microsoft Dynamics CRM MobileMe Node.js NuGet PHP Polly REST Retry Rounding Scrutor Silverlight SimpleMembership sublime-text-2 VS2010 Wistia WooCommerce WordPress WordPress Plugins
  • About
  • Blog
  • Archives
  • Plugins
    • WooCommerce MailChimp
    • Wistia Responsive
  • Contact
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.