C# popular a combo box with variable double

Asked

Viewed 25 times

-2

That make a combo box with various height values but my code does not run, the result only adds 1.47:

for(double i = 1.47; i <= 2.00; i++)
{
   cbxExemplo.Itens.Add(i);
}

2 answers

0


Well your problem is in the incrementer used (i++), since when you perform the operation in the first iteration of the cycle the value is 1.47 so according to the increment i++ the value in the second iteration would be 2.47 and being the condition 2.47 <= 2.00 it stops-te the cycle added only the first value. If you want to use the above code you should use an increment of i+= 0.01

for(double i = 1.47; i <= 2.00; i+=0.01)
{
   cbxExemplo.Itens.Add(i);
}

0

Good after breaking my head a lot and testing various codes I got it I hope it helps someone with the same problem:

for(int i = 147; i <= 200; i++)
{
   double num = i;
   cbxExemplo.Itens.Add((num/100).ToString("N2"));
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.