In elementary school math classes, we were all taught how to round numbers. When given a value of 11.725 and rounding to 2 places, we know to round up to 11.73. Microsoft Dynamics CRM also does rounding for areas that use math, such as calculating the totals on quotes, orders, and so on. However, given the value of 11.725, the rounding in CRM will produce 11.72. Normally, a user would never notice this, as the system rounds consistently in all areas, but if you’re doing some rounding with some JavaScript functions, you may notice that sometimes your values are off by a penny! This is because CRM uses the .NET function System.Math.Round, which produces a rounding variant known as “banker’s rounding.”

What this means is that for the middle value (anything ending in 5) the system rounds the number to the nearest even number. Let’s consider some examples:

• 0.235 rounds to 0.24
• 0.225 rounds to 0.22

The banker’s rounding only applies for values ending in 5, so 6 and above will still always round up (0.226 rounds to 0.23) and 4 and below will always round down. The idea here is that if you consistently round 5 up, over time you will skew your numbers higher than they actually are. Banker’s rounding attempts to mitigate this by pseudo-randomizing the rounding in an attempt to stay closer to “true” over many rounding operations over time.

Additional information on the rounding used in CRM can be found here, with extra information on various rounding techniques here.

As JavaScript uses “Arithmetic Rounding” and has no built-in “banker’s rounding,” we can use the below custom JavaScript function to implement banker’s rounding:

function bankersRounding(num, decimalPlaces) {

var d = decimalPlaces || 0;

var m = Math.pow(10, d);

var n = +(d ? num * m : num).toFixed(8); // Avoid rounding errors

var i = Math.floor(n), f = n – i;

var r = (f == 0.5) ? ((i % 2 == 0) ? i : i + 1) : Math.round(n);

return d ? r / m : r;

}

That’s all there is to it. We hope you enjoyed reminiscing about those grade school math classes! As always, if you need help, the experts at PowerObjects can help.

Happy CRM’ing!

Joe D365

Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.