Frg.RoslynRanger
0.1.2-beta.1
dotnet add package Frg.RoslynRanger --version 0.1.2-beta.1
NuGet\Install-Package Frg.RoslynRanger -Version 0.1.2-beta.1
<PackageReference Include="Frg.RoslynRanger" Version="0.1.2-beta.1" />
paket add Frg.RoslynRanger --version 0.1.2-beta.1
#r "nuget: Frg.RoslynRanger, 0.1.2-beta.1"
// Install Frg.RoslynRanger as a Cake Addin #addin nuget:?package=Frg.RoslynRanger&version=0.1.2-beta.1&prerelease // Install Frg.RoslynRanger as a Cake Tool #tool nuget:?package=Frg.RoslynRanger&version=0.1.2-beta.1&prerelease
Roslyn Ranger
RoslynRanger is a collection of Roslyn analyzers designed to reduce pull request back and forth and help developers avoid common pitfalls in C# and .NET
Math.Round Analyzer
Description
The Math.Round
analyzer warns you when you use Math.Round
without specifying a MidpointRounding
argument. The absence of this argument could lead to rounding behavior that might not be intended.
Pitfall
By default Math.Round
uses rounding strategy MidpointRounding.ToEven
, this is also called Banker's Rounding.
Number | MidpointRounding.ToEven | MidpointRounding.AwayFromZero |
---|---|---|
1.5 | 2.0 | 2.0 |
2.5 | 2.0 | 3.0 |
-1.5 | -2.0 | -2.0 |
-2.5 | -2.0 | -3.0 |
This can cause major issues within any system, especially systems that deal with monetary values.
Example
When Math.Round
is used without explicitly specifying the rounding strategy the analyzer will report a diagnostic.
var result1 = Math.Round(3.14159);
var result2 = Math.Round(3.14159, 2);
The following examples will not report a diagnostic.
var result1 = Math.Round(3.14159, MidpointRounding.AwayFromZero);
var result2 = Math.Round(3.14159, MidpointRounding.ToEven);
var result3 = Math.Round(3.14159, MidpointRounding.ToZero);
var result4 = Math.Round(3.14159, 2, MidpointRounding.ToNegativeInfinity);
var result5 = Math.Round(3.14159, 2, MidpointRounding.ToPositiveInfinity);
Code Fix
This analyzer provides an automatic code fix with defaults to MidpointRounding.AwayFromZero
as the rounding strategy is a commonly expected.
From
var result1 = Math.Round(3.14159);
var result2 = Math.Round(3.14159, 2);
To
var result1 = Math.Round(3.14159, MidpointRounding.AwayFromZero);
var result2 = Math.Round(3.14159, 2, MidpointRounding.AwayFromZero);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.6.0)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.6.0)
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 |
---|---|---|
0.1.2-beta.1 | 94 | 10/12/2023 |
0.1.1 | 153 | 10/12/2023 |