rspec/rules/S2184/csharp/rule.adoc

36 lines
1017 B
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +01:00
When division is performed on ``++int++``s, the result will always be an ``++int++``. You can assign that result to a ``++double++``, ``++float++`` or ``++decimal++`` with automatic type conversion, but having started as an ``++int++``, the result will likely not be what you expect. If the result of ``++int++`` division is assigned to a floating-point variable, precision will have been lost before the assignment. Instead, at least one operand should be cast or promoted to the final type before the operation takes place.
2020-06-30 12:48:07 +02:00
== Noncompliant Code Example
----
static void Main()
{
decimal dec = 3/2; // Noncompliant
Method(3/2); // Noncompliant
}
static void Method(float f) { }
----
== Compliant Solution
----
static void Main()
{
decimal dec = (decimal)3/2;
Method(3.0F/2);
}
static void Method(float f) { }
----
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]