DecimalConversion.Analyzers
.NET 11 changed two things about double and float, with no compatibility switch:
- Conversion to decimal keeps the exact binary value:
(decimal)0.0002 is 0.0002000000000000000095843472, where .NET 10 kept 15 significant digits of a double and 7 of a float (breaking change).
Math.Round(double, digits, mode) rounds the exact binary value: 24409.10 rounded down to cents is 24409.09, and 655.925 rounded to cents away from zero is 655.92 (breaking change).
The .NET 11 SDK's compiler also folds constant casts the new way, whatever the target framework.
Rounding down to cents moves about half of the prices already on the grid, and about half of three-decimal midpoints round the other way (measurements).
This repository has a Roslyn analyzer that reports both, code fixes that keep .NET 10's results, and a Claude Code plugin that migrates a solution with them.
Migrate with Claude Code
/plugin install dotnet11-floating-point --marketplace unsafePtr/DecimalConversion.Analyzers
Then ask Claude to move the solution to .NET 11, or to keep .NET 10's results everywhere.
The skill adds the analyzer for the migration, keeps the sites where .NET 11 changes nothing that matters, has dotnet format fix the rest, and removes the analyzer again.
Only the fixes and one helper file stay.
The plugin carries the package, so no NuGet feed is needed.
Update with claude plugin marketplace update decimal-conversion and claude plugin update dotnet11-floating-point@decimal-conversion.
claude-plugin-eval measures the skill against an agent without it: latest results.
Migrate by hand
The package is not on nuget.org: it ships in this repository, so clone it first.
Copy .editorconfig and Directory.Build.props aside, then add the package to Directory.Build.props:
<PropertyGroup>
<RestoreAdditionalProjectSources>path/to/DecimalConversion.Analyzers/claude-plugin/skills/dotnet11-floating-point/packages</RestoreAdditionalProjectSources>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DecimalConversion.Analyzer" Version="1.3.2" PrivateAssets="all" />
</ItemGroup>
dotnet build --no-incremental lists every site as a DCV warning.
- Copy FloatingPoint.cs into a project that every caller references, so the fix calls it instead of adding a file.
- For each file whose sites need no change, add a section for it to
.editorconfig with dotnet_diagnostic.DCV001.severity = none.
dotnet format analyzers <solution> --diagnostics DCV001 DCV002 DCV003 DCV004 DCV005 --severity warn applies these fixes only, with no whitespace or style changes.
- Test on net10.0 and net11.0, then put the two files back.
Under central package management, write VersionOverride instead of Version.
To keep the analyzer in the build instead, suppress each kept site with #pragma warning disable and its reason.
Rules
| Warning | Reports | Fix |
|---|
| DCV001 | A cast, Convert.ToDecimal, new decimal(...) or decimal.CreateChecked from a double or float | FloatingPoint.ToDecimal(x) |
| DCV002 | A double or float constant cast to decimal | The decimal literal .NET 10 gave |
| DCV003 | Math.Round, MathF.Round, double.Round or float.Round to digits with a directed mode | (double)decimal.Round(FloatingPoint.ToDecimal(x), digits, mode) |
| DCV004 | The same with a nearest mode, ToEven, AwayFromZero or none | The same |
| DCV005 | A decimal sent through a double and back, such as (decimal)Math.Round((double)amount, 2) | FloatingPoint.Normalize(Math.Round(amount, 2)), which keeps 1.20 printing as 1.2, or Math.Round(amount, 2) |
- DCV002's literal is the value .NET 10 folded, from a port of its conversion:
(decimal)Math.E becomes 2.71828182845904m.
decimal.CreateSaturating and CreateTruncating are reported without a fix, since they clamp where the helper throws.
IConvertible.ToDecimal and Convert.ToDecimal(object) on a boxed double or float are not reported.
- The decimal route throws on NaN, infinity and values beyond decimal's range, where
Math.Round returns them, so keep Math.Round for a computed value.
- Rounding to zero digits is not reported, since it did not change.
The helper