-4

I am trying to show the percentage complete in the console:

Console.Clear();
Console.SetCursorPosition(0, 0);
Console.Write("Progress: ");
Console.Write(int_current);
Console.Write(" of ");
Console.Write(int_total);
Console.Write(" ( ");
Console.Write((int_current / int_total) * 100);
Console.Write(" %) ");

As the code runs, I can see the int_current increasing as expected, but the (int_current / int_total) * 100 remains at 0% until int_current and int_total are the same value. At that point, it shows 100%.

What have I done wrong?

oshirowanen
  • 15,297
  • 82
  • 198
  • 350

1 Answers1

2

Use a type converter. When divided by int_total it always returns 0 integer part and returns 0.

            var int_current = 30;
            var int_total = 100;
            Console.Clear();
            Console.SetCursorPosition(0, 0);
            Console.Write("Progress: ");
            Console.Write(int_current);
            Console.Write(" of ");
            Console.Write(int_total);
            Console.Write(" ( ");
            Console.Write(Math.Round(((float)int_current / int_total) * 100));
            Console.Write(" %) ");
Syed Md. Kamruzzaman
  • 979
  • 1
  • 12
  • 33