Back to Devexpress

CRR0020 - Integral divide operation cast to float

coderushforroslyn-118163-static-code-analysis-analyzers-library-crr0020-integral-divide-operation-cast-to-float.md

latest1.3 KB
Original Source

CRR0020 - Integral divide operation cast to float

  • Feb 28, 2025

This analyzer detects division operations in which both parts are of the integral type. In this case, the C# division operation returns an integral result. Since the division of integer numbers can produce a floating point number, the integral result can be unwanted.

csharp
double a = 1 / 7; // CRR0020
Console.WriteLine(a);

// Output: 0
vb
Dim a As Double = 1 \ 7 ' CRR0020
Console.WriteLine(a)

' Output: 0

To fix this in C#, add the type suffix or “.0” to any literal operand. If the division operation includes no literals, cast one or more operands to the required floating-point type. In Visual Basic, you should change the “" operation to “/“.

csharp
double a = 1.0 / 7D;
Console.WriteLine(a);

// Output: 0.142857142857143
vb
Dim a As Double = 1 / 7
Console.WriteLine(a)

' Output: 0.142857142857143

See Also

Suppress Analyzers