Calculating whole percentages

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

In my examples, you can take

int FilesProcessed = 42;
int TotalFilesToProcess = 153;

      

So, first I tried:

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

      

It brought back TotalProgress = 0

Then I tried

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

      

This gives a 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 came here for help?

+3


source to share


2 answers


First go in double

so it doesn't calculate the division between integers

:



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

      

+9


source


int FilesProcessed = 42;
int TotalFilesToProcess = 153;
int TotalProgress = FilesProcessed * 100 / TotalFilesToProcess;

Console.WriteLine(TotalProgress);

      



https://dotnetfiddle.net/3GNlVd

+5


source







All Articles