Skip to main content

CRR0020 - Integral divide operation cast to float

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.

double a = 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 “/“.

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

// Output: 0.142857142857143