How to calculate percentage in C #

I am trying to calculate the percentage of a given long value. The following code returns zero. How to calculate percentage in C #

long value = (fsize / tsize) * (long)100;

      

Here fsize and tsize are multiple Long values.

+3


source to share


3 answers


try it



var value = ((double)fsize / tsize) * 100;
var percentage = Convert.ToInt32(Math.Round(value, 0));

      

+4


source


You can try something like this:

double value = ((double)fsize / (double)tsize) * 100.0;

      



if fsize

int

, and tsize

- int

, then division is also int

, and 0 is the correct answer. You need to convert it to in double

order to return the correct value.

+2


source


the best way to do it

int percentage = (int)Math.Round(((double)current / (double)total) * 100);

      

0


source







All Articles