Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Integer Percentage

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?

like image 915
User1 Avatar asked Nov 12 '14 10:11

User1


People also ask

What is the formula for percentage to number?

If we have to calculate percent of a number, divide the number by the whole and multiply by 100. Hence, the percentage means, a part per hundred. The word per cent means per 100. It is represented by the symbol “%”.

How do you calculate the percentage of data?

Percentage is calculated by taking the frequency in the category divided by the total number of participants and multiplying by 100%. To calculate the percentage of males in Table 3, take the frequency for males (80) divided by the total number in the sample (200). Then take this number times 100%, resulting in 40%.


1 Answers

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

int totalProgress = (int)((double)FilesProcessed / TotalFilesToProcess * 100);
like image 126
ken2k Avatar answered Sep 19 '22 04:09

ken2k