Calculating Integer Percentage

user1 picture user1 · Nov 12, 2014 · Viewed 19.1k times · Source

So I would like to calculate the percentage progress of my program as the nearest integer value

In my examples lets take

int FilesProcessed = 42;
int TotalFilesToProcess = 153;

So First I tried:

Int TotalProgress = ((FilesProcessed / TotalFilesToProcess) * 100)

This returned TotalProgress = 0

Then I tried

Int TotalProgress = (int)((FilesProcessed / TotalFilesToProcess) * 100)

This gives compiler error saying Cannot implicitly convert type decimal to int

Ive tried

Int TotalProgress = Math.Round((FilesProcessed / TotalFilesToProcess) * 100)

and get The call is ambiguous between decimal and double

and so now I've come here for help?

Answer

ken2k picture ken2k · Nov 12, 2014

Cast to double first so it doesn't compute a division between integers:

int totalProgress = (int)((double)FilesProcessed / TotalFilesToProcess * 100);