ExplicitMidpointRoundingAnalyzer 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ExplicitMidpointRoundingAnalyzer --version 1.0.0
                    
NuGet\Install-Package ExplicitMidpointRoundingAnalyzer -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ExplicitMidpointRoundingAnalyzer" Version="1.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ExplicitMidpointRoundingAnalyzer" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="ExplicitMidpointRoundingAnalyzer">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ExplicitMidpointRoundingAnalyzer --version 1.0.0
                    
#r "nuget: ExplicitMidpointRoundingAnalyzer, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ExplicitMidpointRoundingAnalyzer@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ExplicitMidpointRoundingAnalyzer&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=ExplicitMidpointRoundingAnalyzer&version=1.0.0
                    
Install as a Cake Tool

Explicit Midpoint Rounding Analyzer

Roslyn analyzer that reports calls to System.Math.Round and System.MathF.Round when the overload omits an explicit MidpointRounding argument.

Math.Round defaults to MidpointRounding.ToEven, which can be easy to miss in code reviews. This analyzer is intentionally neutral: it only requires the rounding mode to be stated explicitly.

When this can go wrong

The default Math.Round behavior is MidpointRounding.ToEven, also known as banker's rounding. That is a valid rounding strategy, but it can be surprising when the code looks like ordinary "round halves up" logic.

Math.Round(2.5); // 2, not 3
Math.Round(3.5); // 4
Math.Round(4.5); // 4, not 5

This is easy to overlook in user-facing or financial calculations:

var displayedRating = Math.Round(rawRating);
var invoiceLineTotal = Math.Round(quantity * unitPrice, 2);
var taxAmount = Math.Round(netAmount * taxRate, 2);

If the intended policy is "round halves away from zero", the code should say so:

var displayedRating = Math.Round(rawRating, MidpointRounding.AwayFromZero);
var invoiceLineTotal = Math.Round(quantity * unitPrice, 2, MidpointRounding.AwayFromZero);
var taxAmount = Math.Round(netAmount * taxRate, 2, MidpointRounding.AwayFromZero);

Rule

EMRA001 reports a warning for calls such as:

Math.Round(value);
Math.Round(value, 2);
MathF.Round(floatValue);
System.Math.Round(value, 2);

Calls that already specify a rounding mode are ignored:

Math.Round(value, MidpointRounding.ToEven);
Math.Round(value, 2, MidpointRounding.AwayFromZero);

If the intended policy is banker's rounding, make that explicit too:

var invoiceLineTotal = Math.Round(quantity * unitPrice, 2, MidpointRounding.ToEven);

Code fix

The code fix offers both explicit rounding modes:

Math.Round(value);
// option 1
Math.Round(value, MidpointRounding.ToEven);

// option 2
Math.Round(value, MidpointRounding.AwayFromZero);

Build

To create the NuGet package locally:

dotnet pack src/ExplicitMidpointRoundingAnalyzer/ExplicitMidpointRoundingAnalyzer.csproj -c Release -o artifacts
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.11 2,006 7/1/2026
2.0.9 156 6/30/2026
1.0.0 126 6/26/2026