Possible Loss of Fraction in C#

I had written some code in C# to try to account for possible fractions that looked something like this:

double columnCountDividedBy2 = numberOfColumns / 2;

In the above example, numberOfColumns is declared as an int. I thought that, because I was assigning the result to a double, I would get a fractional number if numberOfColumns divided by 2 was not a whole number. However, what was happening was that the fraction was being dropped. ReSharper was also giving me a warning message, "possible loss of fraction".

The reason why (which I have to smack myself now for not realizing) was that, even though I was assigning the result to a double, I was still dividing two integers. Adding a decimal point and zero to the second number of my equation, as shown below, solved the problem:

double columnCountDividedBy2 = numberOfColumns / 2.0;

Comments

I would like to ask you to recommend other sources dedicated to this theme in case you happen to know any of them.

Popular Posts

Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

Silent Renew and the "login_required" Error When Using oidc-client

Fixing the "Please add a @Pipe/@Directive/@Component annotation" Error In An Angular App After Upgrading to webpack 4

How to Determine if a Column Exists in a DataReader