Reputation: 8768
My program cannot determine whether to execute Math.Round
as a decimal
or a double
, but I have no idea how to fix this... Here is my code, though the second to last line is what I am concerned with.
ArrayList topp1 = new ArrayList();
int toppcount = 0;
foreach (Control cb in GroupBoxH1T.Controls)
{
CheckBox cb1 = cb as CheckBox;
if (cb1.Checked == true)
{
toppcount++;
topp1.Add(cb1.Text);
}
}
if (cbhwchoice.Checked == false)
{
ArrayList topp2 = new ArrayList();
foreach (Control cb in GroupBoxH2T.Controls)
{
CheckBox cb1 = cb as CheckBox;
if (cb1.Checked == true)
{
toppcount++;
topp2.Add(cb1.Text);
}
}
toppcount = Math.Round((toppcount/2,MidpointRounding.AwayFromZero);
}
Upvotes: 0
Views: 9439
Reputation: 340
Replace integer 2 with decimal 2.0 in last statement of the if block. So the statement will become like this:
toppcount = Math.Round((toppcount/2.0))
Upvotes: 0
Reputation: 2318
In the second last line
toppcount = Math.Round((toppcount/2,MidpointRounding.AwayFromZero);
toppcount
is integer
2
is also integer
so toppcount/2
will give you integer
as example 1/2
will give you 0
try Convert.ToDecimal(toppcount)/2.0
or
(Decimal)toppcount/2.0
Upvotes: 1
Reputation: 3494
Math.Round
expects a floating point or decimal number because calling it on an integer would have no effect. If you want to call it, pass in a value of that type. To do so, you can simply cast the numerator and denominator to the desired type. For Example:
decimal value = Convert.ToDecimal(toppcount) / 2.0M;
toppcount = Math.Round(value, MidpointRounding.AwayFromZero);
Upvotes: 7